【沫沫金】Sql子查询Not In 无结果缘由

背景源码

select * from ITEM where item_id not in (select parent_item_id from ITEM )

查询效果

【沫沫金】Sql子查询Not In 无结果缘由
无任何匹配值。。。ide

缘由

not in 解析后的执行语句是(id!=1 and id!=2 and id!=null)3d

注意,其中的null。空值存在将会致使条件总体失败,因此无任何匹配数值。code

解决方案

排除null值,便可完成指望结果。blog

select * from ITEM where item_id not in (select parent_item_id from ITEM where parent_item_id is not null)

调整后的Sql,执行效果以下
【沫沫金】Sql子查询Not In 无结果缘由源码


回顾下,not in 是and条件,不能有null值。树结构,父级字段免不了有空值,因此查询不到。排除null值便可。it

相关文章
相关标签/搜索