问题
I am using simple WHERE
clause to fetch records from a table using NotEqualTo
comparison. Ironically it doesn't return recrods where column has no value at all.
Query
SELECT * FROM TABLE WHERE COL<>'Something'
Above query will return all records where COL isNotEqual to Something, however, It doesn't return those where COL is empty or NULL.
Why is that so?
It should return records where COL is empty/null as it still satisfies the condition that it isn't equal to 'Something'
回答1:
Okies thanks guys for helping me out. I didn't know about this behaviour. For others to handle this issue, I modified query as below to get required results:
SELECT * FROM TABLE WHERE COL IS NULL OR COL<>'Something'
回答2:
Check Manual for arithmetic comparison.
You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL.
Check SQL Fiddle
来源:https://stackoverflow.com/questions/24508660/mysql-strange-behaviour-for-comaprison-operator-i-e-notequalto