Sql server 2008 针对不同的条件,将两条update语句合并为一条
我需要一种快速编写更新查询的方法Sql server 2008 针对不同的条件,将两条update语句合并为一条,sql-server-2008,Sql Server 2008,我需要一种快速编写更新查询的方法 UPDATE Content SET Status = 1 WHERE Id in (SELECT userId from [User] where Location = 'US') 在这个查询本身中,我想设置Status=0,其中Id不在(从[User]中选择userId) 基本上,我想将两个更新合并为一个 UPDATE Content SET Status = 1 WHERE Id in (SELECT userId from [User] where L
UPDATE Content
SET Status = 1
WHERE Id in (SELECT userId from [User] where Location = 'US')
在这个查询本身中,我想设置Status=0,其中Id不在(从[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')
,谢谢像这样的方法应该会奏效:
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'
对于每个
内容
行,我们检查是否有相应的US用户,并相应地设置状态
。实际上,在Where子句中,我有很多条件。我必须把这些条件转移到箱子里。它会起作用的。谢谢