Comparisons with NULLs in SQL

雨燕双飞 提交于 2019-11-29 10:51:24

Here is a nice comparison of null handling in SQLite, PostgreSQL, Oracle, Informix, DB2, MS-SQL, OCELOT, MySQL 3.23.41, MySQL 4.0.16, Firebird, SQL Anywhere, and Borland Interbase

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!