创建如下test表,name可为空字段
create table test
(
id bigint,
name varchar(100)
);
现在查询name不等于张三的记录
select * from test where name != '张三'
查询结果
发现最后的查询记录并没有发现id = 3 的记录。
经过验证在 postgresql 、MySQL都会存在这种情况
查postgresql文档发现问题所在
<>
is the standard SQL notation for “not equal”.!=
is an alias, which is converted to<>
at a very early stage of parsing. Hence, it is not possible to implement!=
and<>
operators that do different things.
<>是“不相等”的标准SQL表示法。!=是一个别名,它在解析的早期阶段被转换为<>。因此,在查询name!='张三'就等同于name<>'张三',所以查询结果没有name = null 的记录这就说的通了,查询语句应该改成
select *from test where name != '张三' or name is null