MySQL:为什么在MySQL中忽略NULL? [英] MySQL: Why is NULL ignored in MySQL?

查看:426
本文介绍了MySQL:为什么在MySQL中忽略NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是当我尝试选择不是'CB'的所有值时,查询结果也忽略所有NULL值。



为什么会发生这种情况?

  mysql>从TableName中选择distinct_field_value; 
+ -------------- +
| field_value |
+ -------------- +
| S |
| NULL |
| CA |
| CB |
+ -------------- +
集合中的4行(6.32秒)

mysql>从TableName中选择distinct field_value,其中field_value!='CB';
+ -------------- +
| field_value |
+ -------------- +
| S |
| CA |
+ -------------- +
集合中的2行(0.15秒)

mysql>这是因为与的任何比较,


解决方案

NULL 也会产生 NULL (即不真实也不是假的)。



IS NULL IS NOT NULL 用于对 NULL



因此,您的查询应为:

 。 .. WHERE field_value!='CB'OR field_value IS NULL; 

另请参阅:使用NULL值


I have a table with the following possible values for the column field_value.

But when I try to select all values which are not 'CB', the query result also ignores all NULL values.

Why does this happen?

mysql> select distinct field_value from TableName;
+--------------+
| field_value  |
+--------------+
| S            |
| NULL         |
| CA           |
| CB           |
+--------------+
4 rows in set (6.32 sec)

mysql> select distinct field_value from TableName where field_value!='CB';
+--------------+
| field_value  |
+--------------+
| S            |
| CA           |
+--------------+
2 rows in set (0.15 sec)

mysql>

解决方案

That's because any comparisons with NULL also yield NULL (i.e. not truthy nor falsy).

The special operators IS NULL and IS NOT NULL are used to make useful comparisons against NULL.

Your query should therefore be:

... WHERE field_value!='CB' OR field_value IS NULL;

See also: Working with NULL values

这篇关于MySQL:为什么在MySQL中忽略NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆