我想编写一个查询,该查询返回action1
不是't'
的所有行(包括action1
是null
)
此查询不返回null
记录:
select *
from actions
where action1 <> 't'
此查询确实返回null
记录,但我感到惊讶的是两个谓词都是必要的
select *
from event_actions
where action_c2 is null or action_c2 <> 't'
有没有两个谓词编写此查询的方法?
您可以在比较中使用IS DISTINCT FROM
构造,该构建将null
视为已知值,它与任何非零值不同,即
select *
from actions
where action1 is distinct from 't'
a is distinct from b
等于
case
when a is null then b is null
else a is not null then b is not null and a = b
end
参考:https://wiki.postgresql.org/wiki/is_distinct_from