簡體   English   中英

2個不同的列不在(值)中,忽略NULL值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM