MySQL comparison with null value

我与影子孤独终老i 提交于 2019-11-27 08:35:11
Sam DeHaan

In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Take a look at this MySQL Reference on NULL.

Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.

Any arithmetic comparison with 'NULL' will return false. To check this in SQL:

SELECT IF(NULL=123,'true','false') 

To check NULL values we need to use IS NULL & IS NOT NULL operator.

Alan Fullmer

Based on my tests and the documentation here: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html

You can compare null and get a boolean result using <=>
NOTE: it looks like NOT EQ operator, but it's EQ operator

For example:

select x <=> y; 
or
select @x <=> @y;

This also compares string vs null, string vs string, etc.

In SQL, the NULL value is a special value, not comparable with any other one. The result of a direct comparison with a value is either always FALSE or NULL, depending on the implementation.

To test a null value you should use IS NULL and IS NOT NULL.

SELECT * 
FROM `table_name` 
WHERE IFNULL(`column_name` != 'C', TRUE)

select * from user where application_id='1223333344' and name is null;

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