看起来这应该不难做到,但我缺乏 TSQL 知识。如何解决位列的三个条件?这就是我所拥有的,但编译器当然不喜欢 CASE 中的 OR。
需要明确的是,在“全部”的情况下,我想要零列值和一列值。 Null 将被排除。
我的桌子
Id Name LockedState Deleted
12 Doe, John 0 1
14 Doe, Johnny 0 1
15 Fischer, Julia 1 1
16 Hemsworth, Christopher 0 1
17 Getty, Kristyn 1 1
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND LockState = CASE
WHEN @Assigned = 'ALL' THEN 0 OR 1
WHEN @Assigned = 'Assigned' THEN 1
WHEN @Assigned = 'UnAssigned' THEN 0
END
“全部”所需的输出
12 Doe, John 0 1
14 Doe, Johnny 0 1
15 Fischer, Julia 1 1
16 Hemsworth, Christopher 0 1
17 Getty, Kristyn 1 1
“已分配”的所需输出
15 Fischer, Julia 1 1
17 Getty, Kristyn 1 1
“未分配”的所需输出
12 Doe, John 0 1
14 Doe, Johnny 0 1
16 Hemsworth, Christopher 0 1
你可以使用这样的东西:
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND (@Assigned = 'Assigned' AND LockState = 1
OR @Assigned = 'UnAssigned' AND LockState = 0
OR @Assigned = 'ALL')
或者像这样:
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND LockState = CASE
WHEN @Assigned = 'ALL' THEN LockState
WHEN @Assigned = 'Assigned' THEN 1
WHEN @Assigned = 'UnAssigned' THEN 0
END