【发布时间】:2021-01-15 22:30:56
【问题描述】:
我遇到了空值被反斜杠删除的情况。我没有在网上找到示例,所以我想我会分享它。
create table test(col varchar(10))
insert into test values ('a'),(null),('b')
select * from test where col != '\'
| col |
|---|
| a |
| b |
【问题讨论】:
我遇到了空值被反斜杠删除的情况。我没有在网上找到示例,所以我想我会分享它。
create table test(col varchar(10))
insert into test values ('a'),(null),('b')
select * from test where col != '\'
| col |
|---|
| a |
| b |
【问题讨论】:
这是正确的。几乎所有与NULL 的比较都会导致NULL 被视为错误。标准 SQL 有一个NULL-safe 比较运算符:
select *
from test
where col is distinct from '\';
大多数数据库不支持这一点,因此通常需要进行显式比较:
select *
from test
where col <> '\' or col is null;
【讨论】:
简单的答案是NULL 不能通过除IS NULL 之外的任何表达式。
您需要使用IS NULL,如下:
select * from test where col != '\' or col is null
【讨论】: