我试图识别树上的"叶子",但我不明白为什么我的查询没有给出我想要的.
问题是:
所以我的 idea 是,只要id不在p_id列中,那么它就应该是一片"叶子"
select id, 'Leaf' as type
from Tree
where id not in (select distinct(p_id)
from Tree)
然而,上面的查询没有给我任何结果.
这个解决方案与我的几乎相同,只是它指定p_id不能为NULL,然后返回我想要的.
select id
from Tree
where id not in(select distinct(p_id)
from Tree
where p_id IS NOT NULL)
我很困惑,为什么添加where条款会产生影响?