i've got a table "bla" like this:
[id] [name] [fk]
1 test 4
2 foo 5
3 bar NULL
if i do the sql query
SELECT * FROM bla WHERE fk <> 4
i only get the record with the id 2. i don't get the record with id 3 where fk is null. I thought NULL != 4. Seems that this is wrong.
Why is this so?
NULL
doesn't compare equal to anything. You'll need to accept nulls explicitly:
where fk <> 4 or fk is null;
See Working with NULL for more information about NULL
handling.
NULL
is special in that it represents an "unknown" value. This can't be compared to numbers (or any other value for that matter), hence the result -
Is NULL
<> 4? The answer is - don't know. Is 4 different from an unknown value?
Try this instead:
SELECT * FROM bla WHERE fk <> 4 OR FK IS NULL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With