Postgres有必要在此子句中包含两个谓词



我想编写一个查询,该查询返回action1不是't'的所有行(包括action1null

此查询不返回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

最新更新