Reputation: 241
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?
Upvotes: 2
Views: 60
Reputation: 311978
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, e.g., by using the in
operator:
SELECT *
FROM table1
WHERE field1 IS NULL OR field1 NOT IN ('value1')
Upvotes: 2
Reputation: 198436
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.
Upvotes: 6