当前位置:Gxlcms > 数据库问题 > mysql不等于判断时,空值过滤问题

mysql不等于判断时,空值过滤问题

时间:2021-07-01 10:21:17 帮助过:3人阅读

产生根源

比如我们有三条数据,对应的列名是delete_flag,对应的数据是‘normal’,‘delete’,null。
此时我们查所有不等于delete的记录,我们期望的是两条记录 normal和null。我们书写如下sql。

select * from a where delete_flag != 'delete'

发现查询出来的结果只是一条,这是因为mysql的空值过滤机制。

解决

1、进行一下非null判断

select * from a where IFNULL(delete_flag,'')  != 'delete'

2、将null空值也加入条件

select * from a where delete_flag != 'delete' or delete_flag is null

mysql不等于判断时,空值过滤问题

标签:let   问题   where   null   空值   code   期望   记录   产生   

人气教程排行