遍历问题,可以用Sql实现么?深度遍历优先!
id title pid
0101 a 0
0102 b 0101
0103 c 0101
0104 d 0102
0105 e 0103
0106 f 0104
0107 g 0103
0108 h 0103
0109 i 0108
输入0101
要得到结果:
a b d f
a c e
a c g
a c h i
问题点数:100、回复次数:5Top
1 楼terence4444(T4)回复于 2006-03-17 15:52:19 得分 1
建个存储过程 然后递归就可以了Top
2 楼jilate()回复于 2006-03-17 16:00:52 得分 1
好像看不到什么规律啊Top
3 楼jiezi316(大头仔)回复于 2006-03-17 16:13:27 得分 5
取其所有的子类:
create procedure up_select
@id
as
declare @restb table(id int)
insert into @restb(id) select @id
while exists(select top 1 1 from xx where pid in(select id from @restb) and id not in (select id from @restb))
begin
insert into @restb select id from xx
where pid in(select id from @restb) and id not in (select id from @restb)
end
select title from @restbTop
4 楼bfwqc(丢在风中)回复于 2006-03-17 16:23:26 得分 0
楼上的 我要的是
a b d f
a c e
a c g
a c h iTop
5 楼scmail81(琳·风の狼(修罗))回复于 2006-03-17 16:48:48 得分 93
create table A
(
id varchar(10),
title varchar(1),
pid varchar(10)
)
insert A select '0101','a','0'
insert A select '0102','b','0101'
insert A select '0103','c','0101'
insert A select '0104','d','0102'
insert A select '0105','e','0103'
insert A select '0106','f','0104'
insert A select '0107','g','0103'
insert A select '0108','h','0103'
insert A select '0109','i','0108'
create Function Find_txt(@id varchar(10))
returns @A table(T_txt varchar(1000))
as
begin
declare @T table(id varchar(10),title varchar(1),pid varchar(10), lev int,T_txt varchar(1000))
declare @lev int
set @lev=1
insert @T select id,title,pid,@lev,title from A where id=@id
while @@rowcount>0
begin
set @lev=@lev+1
insert @T select A1.id,A1.title,A1.pid,@lev, T.T_txt+ ' ' + A1.title from A A1, (select * from @T where lev=@lev-1) T where T.id=A1.pid
end
insert @A select T_txt from @T T where (select count(1) from @T where charindex(T.T_txt,T_txt)>0)=1
return
end
select * from dbo.Find_txt('0101')Top




