数据库
首页 > 数据库> > MySQL中的 空值(null)和空字符('')

MySQL中的 空值(null)和空字符('')

作者:互联网

目录

空值(null)和空字符('')是两个比较容易混淆的概念。
本文对两者的区别做简单梳理。

长度和判空

长度 判断空
空值null 长度为null,占空间,但具体占用多大空间不确定。 判断null用is null或is not null
空字符串'' 长度为0,不占用空间。 判断空字符用=''或者!=''

ifnull

对于空值null,可以使用ifnull()函数进行处理。
例如,

select ifnull(comment, '') from orange where id=100;

如果comment字段为null,就返回空字符串。

字段定义为not null,查询的时候也可能返回null

另外,对于字段定义为not null,查询的时候也可能返回null。
例如,
当记录不存在时,查询结果为空:

> select   cluster_name   from  orange      where hostname='123';
Empty set (0.00 sec)

对于查询结果为空,如果使用max()函数,返回值不再是空,字符值为null。

>select       max(cluster_name)     from  orange     where name='123';
+-------------------+
| max(cluster_name) |
+-------------------+
| NULL              |
+-------------------+
1 row in set (0.00 sec)

建议

参考

mysql 空值(null)和空字符('')的区别

标签:name,ifnull,空字符,查询,空值,MySQL,null
来源: https://www.cnblogs.com/lanyangsh/p/16213150.html