【发布时间】:2014-11-20 12:41:12
【问题描述】:
我希望有人可以向我解释以下查询,因为我不熟悉语法:
@[user1] int,
@[user2] varchar(10)
delete
from [table name]
where [id] in (select [id]
from [a diff table name]
where [user1] = @[user1])
and (case
when [user2] = 'some string' then 1
when [table field] = @user2 then 1
else 0
end) = 1
and [other field] = 0
and [other field] = 0
and [other field]= 0
首先,我显然更改了所有变量/表的名称。如果有任何不清楚的地方,请道歉。我知道第一部分是一个删除语句,它使用了一个 in 语句。没关系。我也得到了最后一部分,多个 AND 语句向 WHERE 子句添加条件。
这是我正在努力解决的案例陈述。我有两个问题。
- else 0 有什么用?是否附加到它上面的 when 子句中,以便 if [table field] doesn't = 传入的 user2 变量设置为 0?
- 什么是结尾 = 1?我试过用谷歌搜索,但之前没有找到这种表单的任何示例。
【问题讨论】:
-
我添加了括号,这将有助于更好地解释案例陈述:)
标签: sql-server tsql case