select * from ITEM where item_id not in (select parent_item_id from ITEM )
无任何匹配值。。。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,执行效果以下源码
回顾下,not in 是and条件,不能有null值。树结构,父级字段免不了有空值,因此查询不到。排除null值便可。it