首页 > 解决方案 > 使用每个 sql SELECT 结果,执行 UPDATE

问题描述

我正在尝试为此查询的每个结果编写一个 sql 查询:

select * from table2 where job_id = 'job1' and size=0

应将表 1 中的 job_id 设置为“00”。

到目前为止,我已经尝试过:

update table1 set job_id = '00' 
where id from ( select * from table2 where job_id = 'job1' and size=0 )

我猜应该每个循环都有类似的东西?

标签: sqlsql-serversql-server-2012sql-updatesubquery

解决方案


假设有一个类似于idin的列table1要与子查询的结果进行比较,您可以使用运算符IN

update table1 
set job_id = '00' 
where id in (select id from table2 where job_id = 'job1' and size = 0)

推荐阅读