IS NULL 和 IS NOT DISTINCT FROM NULL 之间有区别吗?

Is there a difference between IS NULL and IS NOT DISTINCT FROM NULL?

value IS NULLvalue IS NOT DISTINCT FROM NULL有区别吗?

每个 SQL 方言都一样吗?

这些测试之间存在细微差别;比较

SELECT ROW(NULL, NULL) IS NOT DISTINCT FROM NULL;

SELECT ROW(NULL, NULL) IS NULL;

IS NOT DISTINCT FROM 谓词是 SQL 标准的一部分 (SQL:2003)
但是,它还没有被所有的 DBMS 完全采用。
实际上,只有几个……好吧,PostgreSql 有。

您可以将其视为 "NULL tolerant equal"

比较:

(1 = 1)  --> true
(1 = 0)  --> false
(1 = null) --> unknown/null
(null = null) --> unknown/null

(1 IS NULL) --> false
(null IS NULL) --> true

(1 IS NOT DISTINCT FROM 1) --> true
(1 IS NOT DISTINCT FROM 0) --> false
(1 IS NOT DISTINCT FROM null)  --> false
(null IS NOT DISTINCT FROM null)  --> true

那么 IS NULLIS NOT DISTINCT FROM 之间的主要区别是什么?
基本上,IS NULL 用于检查元素是否为空。
IS NOT DISTINCT FROM 比较 2 个元素。

在 WHERE 子句中使用,然后是:

WHERE (x IS NOT DISTINCT FROM y)

有标准 SQL 备选方案:

WHERE (x = y OR (x IS NULL AND y IS NULL))