概述
MySql中,null 值意味着"没数据"、"没有值"的意思。
涉及到null值的运算规则
- 比较运算符和算术算数运算符作用于null上,结果恒为null;除0运算发生时,结果也是null;某些函数作用在null上,结果是null,如:concact()函数。
select null = null;
结果:null
select 1 + null;
结果:null
select 1 / 0;
结果:null
select concat('a', null);
结果:null
- mysql中,用0或者null代表false,用非0数字代表true,默认的true用1表示。
select 1 = 1;
结果:1
select *
from t_pos_position
where null;
结果:查不出任何东西
select *
from t_pos_position
where 1 or null;
结果:全表查询
- 在进行 distinct、group by、order by中,null 值被视为是一样的。
在执行order by 时,如果执行的是order by … asc,那么null值将放在最前边,如果执行的是order by … desc,null值将放在最后边。 - 像count()、sum()、min()、max()这些聚合函数如果作用于可能存在null的列上,会自动忽略null值。
- 往timestamp插入null值,则当前时间会被插入,往自增列插入null值,下一个自增值将会被插入。
null值处理
- 判断是否null,用:expr is (not) null 或者 isnull(expr)判断。
- ifnull(expr1, expr2)函数:当expr1是null,那么返回expr2,否则返回expr1。