我有以下格式的表格。我正在尝试查询此表以获取除 PC 和 PT 之外的所有记录。
所以我的查询应该返回 ID 3,4,5,6,7 并且应该忽略 1 和 2
CREATE TABLE #t (ID INT, V VARCHAR(16))
INSERT INTO #t
VALUES (1, 'PC'),
(2, 'PT'),
(3, NULL),
(4, NULL),
(5, NULL),
(6, 'PS'),
(7, 'PD');
drop table #t
我使用的查询仅返回 6 和 7,并忽略结果集中的空记录:
select
id, v
from
#t
where
v != 'PC' and V != PT
请告诉我应该怎么做才能在结果集中包含 null?
最佳答案
您的查询还应该检查 null 条件。
查询 -
select id , v from #t where v not in ('PC' , 'PT') OR v is null
关于sql-server - SQL Server 查询在 where 子句中包含 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763195/