mysql : 使用不等于过滤null的问题

table A 有字段name,age。

 

id name     age

1   null       11

2   jack       12

3   tony      13

 

select * from table A where name != 'tony'

查询结果只有id = 2 的记录;

分析原因:因为 NULL 不是一个「值」,而是「没有值」。「没有值」不满足「值不等于1」这个条件。所以 mysql 尽量不要默认值是 NULL。

要查询出来id = 1的记录有两种方案:

方案一:

          select * from table A where name != 'tony'  or name is null;

方案二:

        select * from table A where IFNULL(name,'')  !=  'tony'

原文地址:https://www.cnblogs.com/enchaolee/p/13673171.html