使用另一个表中的条件进行更新[MySQL]

时间:2017-12-26 19:51:58

标签: mysql join sql-update

我能够从一个表(project_has_tasks)中选择所有记录状态,其中条件来自另一个表,其中progress = 100%

所以:

select t1.status, t2.progress FROM project_has_tasks t1
left join projects t2
on t1.project_id = t2.id
where t2.progress ='100%' AND t1.project_id ='1'

结果我看起来如下:

   | status | progress |
   | open   | 100%     |
   | open   | 100%     |
   | to_do  | 100%     |
   | open   | 100%     |

我想要实现的是更新这些结果,其中进度= 100%完成,因此结果应如下所示:(更新后):

   | status | progress |
   | done   | 100%     |
   | done   | 100%     |
   | done   | 100%     |
   | done   | 100%     |
你能帮我做一下吗? 平台:MySQL

1 个答案:

答案 0 :(得分:1)

您可UPDATE使用JOIN

UPDATE project_has_tasks AS t1
left join projects t2 on t1.project_id = t2.id
SET t1.status = 'done'
where t2.progress ='100%' AND t1.project_id ='1'