Comparisons with NULLs in SQL

后端 未结 2 1757
梦谈多话
梦谈多话 2020-12-19 08:35

ANSI-92 SQL mandates that comparisons with NULL evaluate to \"falsy,\" eg:

SELECT * FROM table WHERE field = NULL
SELECT * FROM table WHERE fiel         


        
2条回答
  •  误落风尘
    2020-12-19 09:20

    For what it's worth, comparing something to NULL is not strictly false, it's unknown. Furthermore, NOT unknown is still unknown.

    ANSI SQL-99 defines a predicate IS [NOT] DISTINCT FROM. This allows you to mix nulls and non-null values in comarisons, and always get a true or false. Null compared to null in this way is true, otherwise any non-null compared to null is false. So negation works as you probably expect.

    PostgreSQL, IBM DB2, and Firebird do support IS [NOT] DISTINCT FROM.

    MySQL has a similar null-safe comparison operator <=> that returns true if the operands are the same and false if they're different.

    Oracle has the hardest path. You have to get creative with use of NVL() or boolean expressions:

    WHERE a = b OR (a IS NULL AND b IS NULL)
    

    Yuck.

提交回复
热议问题