反斜杠过滤器删除的空值

时间:2021-01-15 14:24:23

标签: sql filter null

我遇到了一个空值被反斜杠删除的情况。我没有在网上找到例子,所以我想我会分享它。

create table test(col varchar(10))
insert into test values ('a'),(null),('b')
select * from test where col != '\'
<头>
col
b

2 个答案:

答案 0 :(得分:3)

这是正确的。几乎任何与 NULL 的比较都会导致 NULL 被视为错误。标准 SQL 具有 NULL 安全的比较运算符:

select *
from test
where col is distinct from '\';

大多数数据库不支持这一点,因此通常需要进行显式比较:

select *
from test
where col <> '\' or col is null;

答案 1 :(得分:2)

简单的答案是 NULL 不能通过除 IS NULL 之外的任何表达式。

您需要使用 IS NULL 如下:

select * from test where col != '\' or col is null