在论坛中出现的比较难的sql问题:21(递归问题 检索某个节点下所有叶子节点)

原文: 在论坛中出现的比较难的sql问题:21(递归问题 检索某个节点下所有叶子节点)

最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。


问题:求SQL:检索某个节点下所有叶子节点

部门表名:tb_department
id   int            --节点id
pid int            --父节点id
caption varchar(50) --部门名称
-------------------------------------
id     pid    caption
----------------------------------------------
1       0       AA
20     1      BB
64      20     CC
22     1      DD
23      22     EE
24      1       FF
25     0      GG
26     1      HH
27     25     II
----------------树状结构如下----------------

--------------------------------------
问:怎么检索出某个节点下的所有最尾端的叶子节点。
例如:想检索AA节点下的所有尾端节点CC,EE,FF,HH?

我的解法,适合sql server 2005及以上的 版本:


   
   
  1. create table tb_department(
  2. id int, --节点id
  3. pid int, --父节点id
  4. caption varchar( 50) --部门名称
  5. )
  6. insert into tb_department
  7. select 1 , 0 , 'AA' union all
  8. select 20 , 1 , 'BB' union all
  9. select 64 , 20 , 'CC' union all
  10. select 22 , 1 , 'DD' union all
  11. select 23 , 22 , 'EE' union all
  12. select 24 , 1 , 'FF' union all
  13. select 25 , 0 , 'GG' union all
  14. select 26 , 1 , 'HH' union all
  15. select 27 , 25 , 'II'
  16. go
  17. ;with t
  18. as
  19. (
  20. select id,pid,caption
  21. from tb_department
  22. where caption = 'AA'
  23. union all
  24. select t1.id,t1.pid,t1.caption
  25. from t
  26. inner join tb_department t1
  27. on t.id = t1.pid
  28. )
  29. select *
  30. from t
  31. where not exists( select 1 from tb_department t1 where t1.pid = t.id)
  32. /*
  33. id pid caption
  34. 24 1 FF
  35. 26 1 HH
  36. 23 22 EE
  37. 64 20 CC
  38. */


如果是sql server 2000呢,要怎么写呢:


   
   
  1. --1.建表
  2. create table tb_department(
  3. id int, --节点id
  4. pid int, --父节点id
  5. caption varchar( 50) --部门名称
  6. )
  7. insert into tb_department
  8. select 1 , 0 , 'AA' union all
  9. select 20 , 1 , 'BB' union all
  10. select 64 , 20 , 'CC' union all
  11. select 22 , 1 , 'DD' union all
  12. select 23 , 22 , 'EE' union all
  13. select 24 , 1 , 'FF' union all
  14. select 25 , 0 , 'GG' union all
  15. select 26 , 1 , 'HH' union all
  16. select 27 , 25 , 'II'
  17. go
  18. --2.定义表变量
  19. declare @tb table
  20. ( id int, --节点id
  21. pid int, --父节点id
  22. caption varchar( 50), --部门名称
  23. level int --层级
  24. )
  25. --3.递归开始
  26. insert into @tb
  27. select *, 1 as level
  28. from tb_department
  29. where caption = 'AA'
  30. --4.递归的过程
  31. while @@ROWCOUNT > 0
  32. begin
  33. insert into @tb
  34. select t1.id,t1.pid,t1.caption, level + 1
  35. from @tb t
  36. inner join tb_department t1
  37. on t.id = t1.pid
  38. where not exists( select 1 from @tb t2
  39. where t.level < t2.level)
  40. end
  41. --5.最后查询
  42. select *
  43. from @tb t
  44. where not exists( select 1 from tb_department t1 where t1.pid = t.id)
  45. /*
  46. id pid caption level
  47. 24 1 FF 2
  48. 26 1 HH 2
  49. 64 20 CC 3
  50. 23 22 EE 3
  51. */




发布了416 篇原创文章 · 获赞 135 · 访问量 94万+

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/12020035.html
今日推荐