我有下面的样本数据。
此查询返回我需要的数据:
select * from #temp
where (Field1 = 'Test1' or Field1 = 'Test10') and
Field2 = 'Test2' and
(Field3 = 'Test3' or Field3 = 'Test30')但是,我需要另一个基本上显示不满足上述条件的记录的查询(异常查询)。
如果我使用下面的查询,它不会显示空值。为什么会这样呢?如何获取要显示的具有空值的行?
select * from #temp
where NOT (Field1 = 'Test1' or Field1 = 'Test10') and NOT
Field2 = 'Test2' and NOT
(Field3 = 'Test3' or Field3 = 'Test30')
Create Table #temp
(
ID int,
Field1 varchar(50),
Field2 varchar(50),
Field3 varchar(50)
)
Insert into #temp
(
ID,
Field1,
Field2,
Field3
)
select
1,
'Test1',
'Test2',
'Test3'
union all
select
2,
NULL,
NULL,
NULL
union all
select
3,
'Test',
'Test',
'Test'发布于 2019-08-29 19:09:23
几乎所有与NULL的比较都返回NULL,它被视为false。
一种相当昂贵的方法是使用EXCEPT
select t.*
from #temp t
except
select t.*
from #temp t
where (Field1 = 'Test1' or Field1 = 'Test10') and
Field2 = 'Test2' and
(Field3 = 'Test3' or Field3 = 'Test30');请注意,这会消除重复行。
我会将该条款简化为:
where Field1 in ('Test1', 'Test10') and
Field2 = 'Test2' and
Field3 in ('Test3', 'Test30');显然,您可以重新构造where子句以将null值考虑在内。这真的很麻烦,而且容易出错。因此,另一种方法是创建一个标志并只使用该标志:
select t.*
from #temp t cross apply
(values (case when (Field1 = 'Test1' or Field1 = 'Test10') and
Field2 = 'Test2' and
(Field3 = 'Test3' or Field3 = 'Test30')
then 1 else 0
end)
) v(flag)然后:
where v.flag = 1 -- for inclusion
where v.flag = 0 -- for exclusion发布于 2019-08-29 19:14:06
空值不等于任何值,也不等于任何值。他们是未知的。
要处理空值,可以使用IS NULL和IS NOT NULL。
SELECT * FROM #temp
WHERE (
Field1 NOT IN ('Test1', 'Test10')
OR Field1 IS NULL)
AND (Field2 <> 'Test2' OR Field2 IS NULL)
AND (Field3 NOT IN ('Test3', 'Test30')
OR Field3 IS NULL)作为额外的提示,您可以更改
Field1 = 'Test1' or Field1 = 'Test10'为
Field1 IN ('Test1', 'Test10')https://stackoverflow.com/questions/57708795
复制相似问题