[英]Not in (values) for 2 different columns ignoring NULL value
我有一组值(ID),我想在查询中使用“不在”
如果我用
select ...
where ColumnA not in (1234, ...,...,...,..more than 3000 values -IDs)
它有效,但是...但返回第2行的结果集。
我还有第二个列B,也想在此列中查找查询
ColumnA ColumnB
1234 NULL
NULL 1234
我希望“ not in”出现在两列中,但是我相信NULL值会使我的查询无法正常工作
我希望结果说...没有返回行
OR子句无效:
select ...
where ColumnA not in (1234) or ColumnB not in (1234)
返回第1行和第2行。
尝试这样的事情:
select ColumnA, ColumnB
from SomeDatabase
where ISNULL(ColumnA, 0) not in (1234)
and ISNULL(ColumnB, 0) not in (1234);
我会建议:
select t.*
from table t
where 1234 not in (coalesce(columnA, -1), coalesce(columnB, -1));
这使您只能在查询中包含一次常数值。
declare @ids table
{
id int
}
insert into @ids (id)
values
(1),
(2),
(3),
(4),
...
select *
from table
where
ColumnA is not null and not in (select id from @ids)
and ColumnB is not null and not in (select id from @ids)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.