Update table with join in mysql - php

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

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 Inner Join two tables

** Table Inputs**
(id_inpits, name, quantity_inputs)
Table Outputs
`(id_outputs, name_outputs, quantity_outputs, quantity_disponible)
when I want to update the quantity_output, and calculate the quantity_disponbile
knowing that : quantity_disponbile = quantity_inputs - quantity_output
I tried with :
if(!empty($_POST['do'])) {
$m_id = $_POST['id_output'];
$quantity_outputs = $_POST["quantity_outputs"];
$sql = $db->query("UPDATE outputs AS o INNER JOIN inputs As i ON i.id_input = o.inputs_id SET o.quantity_dispo = 'select quantity_inputs from inputs - FROM (select quantity_outputs from outputs )', o.quantity_outputs = '$quantity_outputs' WHERE o.id_output =' $m_id'");
if(!$sql) {
die(mysql_error());
}
}
One suggestion: quantity_disponbile is supposed a calculated field from both table. So split the logic into two: one select query and one update query. The update query just need to update the quantity_output, and the select query can be like:
select
i.quantity_inputs - o.quantity_output = quantity_disponible
from output o
inner join input i on i.id_inpits = o.inputs_id
I don't correct the typo anyway

Need to correct mysql query

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

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

Only use AND statment when if is true in mysql query

I have updated an old system and I have a query problem.
To get to business this is the direct problem:
There are new records witch are associated with an id to other records. There are also old records which are not.
Case1 : there is a INT id and when that is present the query has to use AND after an include.
Case 2 : when the value of the INT id is 0 it has to do nothing
This is the part of the query where I need to make an variable AND statement:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id AND i.id = v.inkoop_id
I never used IF stamens inside a query but i am looking for something like this:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id if(v.inkoop_id > 0){AND i.id = v.inkoop_id}
Try this:
LEFT JOIN table v
ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND (v.inkoop_id <= 0 OR i.id = v.inkoop_id)
How about this....
LEFT JOIN table v ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND ((v.inkoop_id > 0 AND i.id = v.inkoop_id) OR (v.inkoop_id = 0))

Categories