Why does SELECT IF(null=null, 'true', 'false'); return false but SELECT IF(1=1, 'true', 'false'); return true in mysql?
1 Answer
NULL in SQL is a placeholder for an unknown value. Because it is unknown it cannot be compared to other values, not even with NULL.
Read "unknown" every time you encounter NULL in a query and it will make more sense.
Regarding MySQL, the behaviour of NULL is documented: https://dev.mysql.com/doc/refman/5.7/en/working-with-null.html
2 Comments
Wafula Samuel
it was indeed
surprising as mentioned in the documentation 😂. Thanks thoughaxiac
The programming languages that provide the concept of
NULL give it a different semantic. In such languages NULL is equal to NULL and in some of them (PHP, JavaScript etc.) it is even equal to 0 or ''.
NULLmeans "unknown value" or "anything".