一个关于无限层查询的问题

j99616 2003-12-05 03:50:07
编号 名称 隶属
1 aa 0
2 bb 1
3 c 1
4 dd 2
5 ee 3
6 ww 2
7 qq 4
8 ff 4
..................

这是一个表大概的结构,我怎么才能选出编号为2的下面的所有的隶属子记录呢?

也就是要选出4,6,7,8(是要做无限层的,不限于3层)

大家有没有好办法

...全文
35 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrazyFor 2003-12-05
  • 打赏
  • 举报
回复
http://search.csdn.net/expert/topic/57/5704/2003/1/20/1375432.htm
j99616 2003-12-05
  • 打赏
  • 举报
回复
十分感谢,对二位,我也是早就佩服得.......
如果二为肯个面子,我希望大家交一个朋友
QQ:158306893
MSN:LJ99616@MSN.COM

再次表示感谢!!!!
zjcxc 2003-12-05
  • 打赏
  • 举报
回复
参考我的贴子:

树形数据处理:
http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.235531
zjcxc 2003-12-05
  • 打赏
  • 举报
回复
--下面是数据测试

--测试数据
create table tb(编号 int,名称 varchar(10),隶属 int)
insert into tb
select 1,'aa',0
union all select 2,'bb',1
union all select 3,'c',1
union all select 4,'dd',2
union all select 5,'ee',3
union all select 6,'ww',2
union all select 7,'qq',4
union all select 8,'ff',4

go

--创建函数,得到指定编号及其下属列表
create function f_getcid(@编号 int)
returns @t table(id int,level int)
as
begin
declare @level int
set @level=1
insert @t select 编号,@level from tb where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from tb a join @t b on a.隶属=b.ID
where b.level=@level-1
end
return
end
go

--得到结果
select a.* from tb a join dbo.f_getcid(2) b on a.编号=b.id

go
--删除测试环境
drop table tb
drop function f_getcid

/*--测试结果
编号 名称 隶属
----------- ---------- -----------
2 bb 1
4 dd 2
6 ww 2
7 qq 4
8 ff 4

(所影响的行数为 5 行)
--*/
zjcxc 2003-12-05
  • 打赏
  • 举报
回复
--为方便处理,可以写成函数

--创建函数,得到指定编号及其下属列表
create function f_getcid(@编号 int)
returns @t table(id int,level int)
as
begin
declare @level int
set @level=1
insert @t select 编号,@level from tb where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from tb a join @t b on a.隶属=b.ID
where b.level=@level-1
end
return
end
go

--得到结果
select a.* from tb a join dbo.f_getcid(2) b on a.编号=b.id
zjcxc 2003-12-05
  • 打赏
  • 举报
回复
--下面是数据测试

--测试数据
declare @tb table(编号 int,名称 varchar(10),隶属 int)
insert into @tb
select 1,'aa',0
union all select 2,'bb',1
union all select 3,'c',1
union all select 4,'dd',2
union all select 5,'ee',3
union all select 6,'ww',2
union all select 7,'qq',4
union all select 8,'ff',4

--查询处理
declare @编号 varchar(100)
set @编号=2

declare @t table(id int,level int)
declare @level int
set @level=1
insert @t select 编号,@level from @tb where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from @tb a join @t b on a.隶属=b.ID
where b.level=@level-1
end

--得到结果
select a.* from @tb a join @t b on a.编号=b.id

/*--测试结果
编号 名称 隶属
----------- ---------- -----------
2 bb 1
4 dd 2
6 ww 2
7 qq 4
8 ff 4

(所影响的行数为 5 行)
--*/
zjcxc 2003-12-05
  • 打赏
  • 举报
回复
declare @编号 varchar(100)
set @编号=2

declare @t table(id int,level int)
declare @level int
set @level=1
insert @t select 编号,@level from 表 where 编号=@编号
while @@rowcount>0
begin
set @level=@level+1
insert @t select a.编号,@level from 表 a join @t b on a.隶属=b.ID
where b.level=@level-1
end

select * from @t
pengdali 2003-12-05
  • 打赏
  • 举报
回复
/*--按父找子--*/
declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
insert @a values(1,0,'中国')
insert @a values(2,0,'美国')
insert @a values(3,0,'加拿大')
insert @a values(4,1,'北京')
insert @a values(5,1,'上海')
insert @a values(6,1,'江苏')
insert @a values(7,6,'苏州')
insert @a values(8,7,'常熟')
insert @a values(9,6,'南京')
insert @a values(10,6,'无锡')
insert @a values(11,2,'纽约')
insert @a values(12,2,'旧金山')

declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200),lev int)
insert @tmp1 select *,1 from @a where tc_ID=1
while @@rowcount>0
insert @tmp1 select a.*,1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1)
select * from @tmp1
pengdali 2003-12-05
  • 打赏
  • 举报
回复
[树]
http://expert.csdn.net/Expert/TopicView1.asp?id=2285830
pengdali 2003-12-05
  • 打赏
  • 举报
回复
[树]
http://expert.csdn.net/Expert/TopicView1.asp?id=2285830

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧