简体   繁体   中英

Why doesn't SQL “NOT IN ('val1')” return if the field is null?

I have a query like this one:

Select *
FROM table1
WHERE field1 NOT IN ('value1')

If the field1 of the row is null , it will not be returned. It doesn't look logical to me, because null is not 'value1', so the row should be returned, this can cause many bugs. I understand that it's working like that because of some reasons. What are they?

A NULL is not a value, in SQL.

SELECT NULL = NULL
# => NULL
SELECT NULL != NULL
# => NULL

Thus,

cause null is not 'value1'

but NULL is also not not 'value1'. NULL is basically the SQL way of saying "I don't know". So it might be 'value1', or it might not. The way to test for NULL is

SELECT NULL IS NULL;
# => 1

Thus, try this:

WHERE field1 NOT IN ('value1') OR field1 IS NULL

to specifically address the case.

null is not a value - it's the lack thereof. Think of it as missing data. Whenever it participates in a logical operator that works on values (such as in ), it's result is "unknown" - "Question: is a missing value one the the following values? Answer: I don't know".

Since "unknown" ins't true, the row is not evaluated. If you want to handle null s you'd have to do so explicitly, eg, by using the in operator:

SELECT *
FROM   table1
WHERE  field1 IS NULL OR field1 NOT IN ('value1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM