2

NOT IN 子句在检查条件时省略了NULL值。

INPUT  
ID  NAME  
1   A  
2   <null>
3   C

SELECT... FROM...
WHERE NAME NOT IN ('C')

只返回 ID 值 1。我需要 1 和 2。

这可以做到吗?

4

4 回答 4

5
WHERE NAME NOT IN ('C') OR NAME IS NULL
于 2013-07-09T09:23:22.077 回答
3

要么你检查 NULL 值

select *
from not_in
where name not in ('C') or name is null;

或者您可以使用合并转换任何其他字符中的 NULL 值。我在下面的示例中使用 ' '。

select *
from not_in
where coalesce(name, ' ') not in ('C');
于 2013-07-09T09:36:41.840 回答
2

Correct SQL would be

SELECT... FROM...
WHERE NAME NOT IN ('C')
or NAME is NULL

That's because any comparisons with NULL also yield NULL (i.e. not truthy nor falsy). (Credit to @Jack)

Refer:
MySQL: Why is NULL ignored in MySQL?
Working of Null Values

于 2013-07-09T09:23:18.487 回答
0
SELECT... 
FROM...
WHERE NAME NOT IN ('C') 
    OR NAME IS NULL

SELECT... 
FROM...
WHERE ISNULL(NAME, '') NOT IN ('C') 
于 2013-07-09T09:23:16.230 回答