Need to correct mysql query - php

UPDATE student
INNER JOIN
fee_head2 ON (student.new_old = fee_head2.new_old)
INNER JOIN
fee_head2 ON (student.class = fee_head2.class)
SET
student.head1_apr = fee_head2.head1_apr
This is showing #1066 - Not unique table/alias: 'fee_head2'.
Can anyone correct this query for me?

No need to JOIN the same table again, try this:
UPDATE student INNER JOIN fee_head2
ON (student.new_old = fee_head2.new_old)
AND (student.class = fee_head2.class)
SET student.head1_apr = fee_head2.head1_apr

Aziz beat me to the obvious naswer.
UPDATE student SET student.head1_apr =
CASE
WHEN student.new_old = fee_head2.new_old AND student.class = fee_head2.class THEN fee_head2.head1_apr
ELSE NULL
END

Related

Comparing Two tables for data relation

My SQL query is:
UPDATE REGISTRUDENT set qual=1
WHERE studentolevelsubjects AS sls
INNER JOIN courserequirements AS csreq ON sls.subject_id = csreq.subject_id
WHERE sls.stud_id = '$stud_id' AND sls.grade_id>=csreq.min_grade AND sls.examno = '$examno' AND csreq.course_id = '$course_id'
I'm having issue comparing two different table to get if a registrant qualified based on the course requirement I need help
course requirement table
The subject passed by the student in exams
I intend updating and setting the qual =1 if the student qualifies
Many Thanks
Your query syntax is not correct. It rather should be
UPDATE REGISTRUDENT rs
JOIN studentolevelsubjects sls ON sls.some_column = rs.some_column //missing this JOIN
JOIN courserequirements csreq ON sls.subject_id = csreq.subject_id
AND sls.grade_id >= csreq.min_grade
WHERE sls.stud_id = '$stud_id'
AND sls.examno = '$examno'
AND csreq.course_id = '$course_id'
SET rs.qual = 1;

Update table with join in mysql

i want to update field in my database.
I have two table and table field like bellow
Table operations_per_assembly Field operation_id,is_mecahnical
Table operations Field id,repair_type_id
Now i want to update is_mechanical field where repair_type_id = 3
My query
UPDATE
`operations_per_assembly`
JOIN `operations`
ON `operations`.`id` = `operations_per_assembly`.`operation_id`
SET `operations_per_assembly`.`is_mechanical` = '4'
WHERE `operations_per_assembly`.`operation_id` = `operations`.`id`
AND `operations_per_assembly`.repair_type_id = 3
Please help me.
Put the repair_type_id = 3 condition in the join conditions. This way you are telling to join only on repair_type_id = 3 so you will only get those records.
UPDATE
`operations_per_assembly`
JOIN `operations`
ON `operations`.`id` = `operations_per_assembly`.`operation_id` AND `operations`.repair_type_id = 3
SET `operations_per_assembly`.`is_mechanical` = '4'
UPDATE `operations_per_assembly` a
JOIN `operations` b
ON a.operation_id = b.id
SET a.is_mechanical = '4'
WHERE codition (user proper condition)
In the WHERE clause you are using operations_per_assembly.repair_type_id but your operations_per_assembly table does not have repair_type_id in it.
So try the below query:
UPDATE `operations_per_assembly` PAS
JOIN `operations` OPE ON OPE.`id` = PAS.`operation_id`
SET PAS.`is_mechanical` = '4'
WHERE OPE.repair_type_id = 3

php pdo where clause

I have the following code for selecting from multiple tables where the order number matches.
$orderNumber = $_GET['orderNumber'];
$sql = $db->prepare("
SELECT
*
from `KC_Orders`
INNER JOIN
`KC_Payments`
on KC_Orders.orderNumber = KC_Payments.orderNumber
INNER JOIN
`KC_OrderStatus`
on KC_Orders.orderNumber = KC_OrderStatus.orderNumber
INNER JOIN
`KC_Statuses`
on KC_OrderStatus.statusID = KC_Statuses.statusID
WHERE
orderNumber= :orderNumber");
$sql->execute(array(':orderNumber' => $orderNumber));
$orderInfo = $sql->fetchAll();
Now when I var_dump($orderInfo); it returns: array(0) { } What is wrong? All the tables include the same $orderNumber field within it. If I take the WHERE part out it works just fine except it returns every row not just one. (obviosly).
Please help us!
You need to specify what the OrderNumber is from, and the * is from (what table). So try this new code:
$sql = $db->prepare("SELECT KC_Orders.* from `KC_Orders` INNER JOIN `KC_Payments` on KC_Payments.orderNumber = KC_Orders.orderNumber INNER JOIN `KC_OrderStatus` on KC_OrderStatus.orderNumber = KC_Order.orderNumber INNER JOIN `KC_Statuses` on KC_Statuses.statusID = KC_OrderStatus.statusID WHERE KC_Orders.orderNumber= :orderNumber");
Ordernumber in where clause should have table prefix if it exists in multiple tables
Try this
$orderNumber = (int)$_GET['orderNumber'];
Then where :ordernumber is put $orderNumber
Then execute

How to query 2 tables

I need to sum the transactions in tblgl (tblgl.SUM(InMonthActual)) for a selection of cost centres (tblgl.CostCentreCode) where the following conditions are met:
tblgl.PeriodNumber = 2
tblgl.CostCentreCode = tblcostcentrehierarchy.CostCentreCode
WHERE tblcostcentrehierarchy.Level7 = "RWK312 CORPORATE"
tblgl.CostCentreCode = tblcostcentreallocations.CostCentreCode
WHERE tblcostcentreallocations.Username = "jonest"
At the moment I'm running 3 separate queries to create an array which is used in the next query.
Is there a way to do it in one (maybe using JOIN)?
I hope this query will fetch your desire data. Check and let me know if it works for you.
SELECT SUM(tb1.`InMonthActual`)
FROM `tblgl` as tb1
JOIN `tblcostcentrehierarchy` as tb2 ON tb1.`CostCetntreCode` = tb2.`CostCentreCode`
JOIN `tblcostcentreallocations` as tb3 ON tb1.`CostCetntreCode` = tb3.`CostCentreCode`
WHERE tb1.`PeriodNumber` = '2' AND tb2.`Level17` = "RWK312 CORPORATE" AND tb3.`Username` = "jonest"
Give it a try
SELECT SUM(tblgl.InMonthActual) FROM tblgl
INNER JOIN tblcostcentrehierarchy ON (tblgl.CostCentreCode = tblcostcentrehierarchy.CostCentreCode AND tblgl.PeriodNumber = 2)
INNER JOIN tblcostcentreallocations ON (tblgl.CostCentreCode = tblcostcentreallocations.CostCentreCode)
WHERE tblcostcentreallocations.Username = "jonest" AND tblcostcentrehierarchy.Level7 = "RWK312 CORPORATE"
GROUP BY tblgl.InMonthActual
Hope it works fine

Error handling in the following sql query

Fiddle with tables here
I'm using the following sql with the tables in the fiddle to check if a user has reached the borrowing limit. The problem here is, If an invalid item number were supplied it returns NULL, if a user has not borrowed any items, it returns NULL. This way, I cannot tell if a invalid item number were supplied or if a user actually has not borrowed any books. What would be a good way to check if a invalid item number was supplied or a member actually has not borrowed anything under that category?
set #mId = 3 //Has not borrowed anything till now.
set #id = 21; //This item does not appear in the collection_db table and is therefore invalid.
set #country = 'US';
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder and col1.country = lend.country
WHERE col1.id = #id and col1.country = #country
AND col2.category = 10
AND lend.memId = #mId and lend.country = #country
The furthest I could get with the one query is (had to take out php and "country" vars for fiddle to work):
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
,case when valid1.id is not null then 'true' else 'false' end as validId
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder,(
Select Distinct a.id From collection_db a
Where a.id = 4) valid1
WHERE col1.id = 4
AND col2.category = 10
AND lend.memId = 1
You may have to do a preparatory query checking for a valid memId:
$theQuery = "SELECT DISTINCT memId FROM lendings WHERE memId = 1"
Then test it here:
if (mysql_num_rows(mysql_query($theQuery)) <= 0) { /* No memId exists */ }
else { /* Do big query here */ }
You can use a tableA LEFT JOIN tableB, which will return results for the tableA even if tableB has no matches and will return NULL values for those in tableB.
Unfortunately, I can't quite figure out where you need LEFT JOINS, but probably you want them in both places.
You also might have to reorder the tables if it is the first table that should be on the right side of a LEFT JOIN. You could use a RIGHT JOIN but it is less readable to me.
maybe you should try "left join" if col1 do not have too much data,or do the query step by step

Categories