NOT IN 子句在检查条件时省略了NULL值。
INPUT
ID NAME
1 A
2 <null>
3 C
SELECT... FROM...
WHERE NAME NOT IN ('C')
只返回 ID 值 1。我需要 1 和 2。
这可以做到吗?
WHERE NAME NOT IN ('C') OR NAME IS NULL
要么你检查 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');
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
SELECT...
FROM...
WHERE NAME NOT IN ('C')
OR NAME IS NULL
SELECT...
FROM...
WHERE ISNULL(NAME, '') NOT IN ('C')