在论坛中出现的比较难的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及以上的 版本:

create table tb_department(
id   int,            --节点id
pid int,            --父节点id
caption varchar(50) --部门名称
)

insert into tb_department
select 1       ,0      	,'AA' union all
select 20     	,1      	,'BB' union all
select 64      ,20     	,'CC' union all
select 22     ,	1      	,'DD' union all
select 23     , 22     	,'EE' union all
select 24    ,  1      	,'FF' union all
select 25     ,	0      	,'GG' union all
select 26    , 	1      	,'HH' union all
select 27    , 	25     	,'II'
go


;with t
as
(
select id,pid,caption
from tb_department
where caption = 'AA'

union all

select t1.id,t1.pid,t1.caption
from t
inner join tb_department t1
        on t.id = t1.pid
)

select *
from t
where not exists(select 1 from tb_department t1 where t1.pid = t.id)
/*
id	pid	caption
24	1	FF
26	1	HH
23	22	EE
64	20	CC
*/


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

--1.建表
create table tb_department(
id   int,            --节点id
pid int,            --父节点id
caption varchar(50) --部门名称
)

insert into tb_department
select 1       ,0      	,'AA' union all
select 20     	,1      	,'BB' union all
select 64      ,20     	,'CC' union all
select 22     ,	1      	,'DD' union all
select 23     , 22     	,'EE' union all
select 24    ,  1      	,'FF' union all
select 25     ,	0      	,'GG' union all
select 26    , 	1      	,'HH' union all
select 27    , 	25     	,'II'
go


--2.定义表变量
declare @tb table
(id   int,            --节点id
pid int,            --父节点id
caption varchar(50), --部门名称
level int       --层级
)
  

--3.递归开始  
insert into @tb 
select *,1 as level
from tb_department
where caption = 'AA'


--4.递归的过程
while @@ROWCOUNT > 0
begin
    
    insert into @tb
	select t1.id,t1.pid,t1.caption,level + 1
	from @tb t
	inner join tb_department t1
			on t.id = t1.pid
    where not exists(select 1 from @tb t2 
                     where t.level < t2.level)
end


--5.最后查询
select *
from @tb t
where not exists(select 1 from tb_department t1 where t1.pid = t.id)
/*
id	pid	caption	level
24	1	FF	2
26	1	HH	2
64	20	CC	3
23	22	EE	3
*/




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值