Postgres是否有必要在此where子句中包含两个谓词?

时间:2019-02-27 17:10:21

标签: postgresql

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

此查询不返回null记录:

select * 
from actions 
where action1 <> 't'

此查询的确返回了null条记录,但令我惊讶的是两个谓词都是必需的

select *
from event_actions
where action_c2 is null or action_c2 <> 't'

是否可以在没有两个谓词的情况下编写此查询?

1 个答案:

答案 0 :(得分:1)

您可以在比较中使用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