日期:2014-05-18  浏览次数:20608 次

SQL 判断一个树形目录是否还有下层目录?见下表数据:
id是目录 parentid是上层目录,我现在想判断id的下层还有没有目录的,即当前id是最底层了

id parentid
1 root
2 1
3 1
4 2
5 2
6 4

树状结构是这样的:
root
 --1
  --2
  --3
 --2
  --4
  --6
  --5

现在想判断id为2的下层还有目录4和6,即2不是最下层目录,怎样判断?

------解决方案--------------------
select *
from T
where id not in
(
select parentid
from T
)