首页 > 解决方案 > SQL:“条件不为真”模式替代“为空或不是(条件)”

问题描述

在 SQL(特别是 Postgres)中:

where not foo='bar'case is null的子句foo评估为某种null,导致该行不包含在结果中。

另一方面,where (foo='bar') is not truewhere foois null 子句的计算结果为true(该行包含在结果中)。

那么使用(foo='bar') is not true等价于foo is null or not foo='bar'吗?

似乎人们出于某种原因没有使用这种模式,我错过了什么吗?

标签: sqlpostgresqlnullcomparison

解决方案


那么使用(foo='bar') is not true等价于foo is null or not foo='bar'吗?

是的。或更简单:

foo IS NULL OR foo <> 'bar'

或者更简单,但是:

foo IS DISTINCT FROM 'bar'

使用后者。
手册章节比较函数和运算符中的详细信息。
有关的:


推荐阅读