MySQL strange behaviour for comaprison operator (!=/<>) i.e. NotEqualTo

微笑、不失礼 提交于 2019-12-13 19:40:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!