我需要一种快速编写更新查询的方法
UPDATE Content
SET Status = 1
WHERE Id in (SELECT userId from [User] where Location = 'US')
在这个查询本身中,我想SET Status = 0 WHERE Id NOT IN(从[User]中选择userId)。
基本上,我想将两个更新合并为一个。
UPDATE Content
SET Status = 1
WHERE Id in (SELECT userId from [User] where Location = 'US')
AND
UPDATE Content
SET Status = 0
WHERE Id NOT in(SELECT userId from [User] where Location = 'US')
,由于
答案 0 :(得分:3)
这样的事情应该有效:
update c
set Status = case when u.userId is not null then 1 else 0 end
from Content c
left join [User] u on c.id = u.userId and u.Location = 'US'
对于每个Content
行,我们会检查是否有相应的美国用户,并相应地设置Status
。