当前位置:Gxlcms > 数据库问题 > Mysql Not in有null值查询的问题

Mysql Not in有null值查询的问题

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

* from A where id not in (select fid from B).

发现查询结果无论如何都是0条记录。后来发现B里面返回的查询结果集有一条NULL值,查了资料才知道mysql 的not in里面如果有一个NULL值,将返回0条记录。

要解决这个问题需要把 select fid from B 变成

select fid from B where B.fid is not null

这个问题在其他的数据库里也会有,是因为 not in的处理有问题。再具体详细说明一下,如下:

select * from A where id not in (1,2,null )

那么就没有记录返回,因为里面有null值,当用id和null比较的时候,会返回false,所以一条记录都不会返回,所以要改成下面就可以了:

select * from A where id not in (select bid from B where bid  is not null ) 

Mysql Not in有null值查询的问题

标签:

人气教程排行