简体   繁体   English

在mysql中使用join更新表

[英]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 operations_per_assembly 字段 operation_id,is_mecahnical

Table operations Field id,repair_type_id operations 字段 id,repair_type_id

Now i want to update is_mechanical field where repair_type_id = 3 现在我想更新is_mechanical字段,其中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. 将repair_type_id = 3条件放入联接条件中。 This way you are telling to join only on repair_type_id = 3 so you will only get those records. 这样,您告诉您仅在repair_type_id = 3上加入,因此您只会获得这些记录。

  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 . WHERE子句中,您正在使用operations_per_assembly repair_type_id but your operations_per_assembly table does not have repair_type_id in it. repair_type_id但您的operations_per_assembly表中没有repair_type_id

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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