Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 针对不同的条件,将两条update语句合并为一条_Sql Server 2008 - Fatal编程技术网

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子句中,我有很多条件。我必须把这些条件转移到箱子里。它会起作用的。谢谢