0

Why does SELECT IF(null=null, 'true', 'false'); return false but SELECT IF(1=1, 'true', 'false'); return true in mysql?

2
  • Because NULL means "unknown value" or "anything". Commented Dec 9, 2018 at 15:18
  • @axiac Thanks about that. I encountered that because in a programming language e.g python, it's true Commented Dec 9, 2018 at 15:21

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

it was indeed surprising as mentioned in the documentation 😂. Thanks though
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 ''.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.