简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM