1

我需要一种快速编写更新查询的方法

UPDATE Content
SET Status = 1
WHERE Id in (SELECT userId from [User] where Location = 'US')

在此查询本身中,我想 SET Status = 0 WHERE Id NOT IN(SELECT userId from [User])。

基本上,我想将两个更新合二为一。

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')

,谢谢

4

1 回答 1

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

于 2013-09-25T11:10:44.633 回答