找一个递归写法,100分!

weinaxxc 2008-12-19 09:32:00
--------------------------数据---------------------------------------
id fid
1 0
2 1
3 2
4 2
5 4
--------------------------查询结果---------------------------------------
id ids
1 1,2,3,4,5
2 2,3,4,5
3 3
4 4,5

咋办呢?
...全文
147 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiahao_li 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dobear_0922 的回复:]
SQL code--> By dobear_0922(小熊) 2008-12-19 09:44:50
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[fid] int)
insert [tb]
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,2 union all
select 5,4

--select * from [tb]

;with Tree as
(
select id, ids=id from tb
union all
select Tree.id, ids=tb.id

[/Quote]

利用SQL 2005的用特性CTE实现递归。正解。。。
claro 2008-12-19
  • 打赏
  • 举报
回复
帮顶
yygyogfny 2008-12-19
  • 打赏
  • 举报
回复
学习
中国风 2008-12-19
  • 打赏
  • 举报
回复
use tempdb
go
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[fid] int)
insert [tb]
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,2 union all
select 5,4

go

create function f_tree(@ID int)
returns nvarchar(100)
as
begin
declare @s nvarchar(100),@fid int
select @s=isnull(@s+',','')+rtrim([id]),@fid=[id] from tb where fid=@ID

if exists(select 1 from tb where fid=@fid)
return @s+','+dbo.f_tree(@fid)
return @s
end

go

select
[id],
[fid],
rtrim([id])+isnull(','+dbo.f_tree([id]),'') as [path]
from
[tb]
id fid path
----------- ----------- -----------------------------------------------------------------------------------------------------------------
1 0 1,2,3,4,5
2 1 2,3,4,5
3 2 3
4 2 4,5
5 4 5

(5 個資料列受到影響)


2000可用(層數不超過32)
dobear_0922 2008-12-19
  • 打赏
  • 举报
回复
--> By dobear_0922(小熊) 2008-12-19 09:44:50
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[fid] int)
insert [tb]
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,2 union all
select 5,4

--select * from [tb]

;with Tree as
(
select id, ids=id from tb
union all
select Tree.id, ids=tb.id
from Tree join tb on tb.fid=Tree.ids
)
select id,ids=STUFF((SELECT ','+rtrim(ids) FROM Tree t WHERE id=tb.id FOR XML PATH('')), 1, 1, '')
from Tree tb
group by id
/*

(5 行受影响)
id ids
----------- -----------------
1 1,2,3,4,5
2 2,3,4,5
3 3
4 4,5
5 5

(5 行受影响)


*/
drop table tb
dobear_0922 2008-12-19
  • 打赏
  • 举报
回复
--> By dobear_0922(小熊) 2008-12-19 09:44:50
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[fid] int)
insert [tb]
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,2 union all
select 5,4

--select * from [tb]

;with Tree as
(
select id, ids=id from tb
union all
select Tree.id, ids=tb.id
from Tree join tb on tb.fid=Tree.ids
)
select id,ids=STUFF((SELECT ','+rtrim(ids) FROM Tree t WHERE id=tb.id FOR XML PATH('')), 1, 1, '')
from Tree tb
group by id
/*

(5 行受影响)
id ids
----------- -----------------
1 1,2,3,4,5
2 2,3,4,5
3 3
4 4,5
5 5

(5 行受影响)


*/
drop table tb
Andy__Huang 2008-12-19
  • 打赏
  • 举报
回复
参考:
-- DROP TABLE Dept
CREATE TABLE dbo.Dept(
id int identity(1,1),
Parent numeric(2),
Name varchar(1),
Type numeric(1)
)

INSERT INTO Dept(Parent,Name,Type)
select 0,'A',1
union all select 1,'B',2
union all select 1,'C',2
union all select 2,'D',3
union all select 2,'E',3
union all select 2,'F',3
union all select 3,'G',2
union all select 7,'H',4
union all select 8,'I',4

SELECT * FROM Dept


-- drop function dbo.fn_string
create function dbo.fn_string(@id numeric(2))
returns varchar(100)
as
begin
declare @s1 varchar(100)
declare @parent1 numeric(2),@name1 varchar(1),@type1 numeric(2)
set @s1=''
select @parent1=parent,@name1=name,@type1=type from Dept where id=@id
if @type1<=2
begin
select @s1=@name1
end
else
begin
select @s1=dbo.fn_string(@parent1)+'\'+@name1
end
return(@s1)

end

SELECT ID,Name,FullPath=dbo.fn_string(id) FROM Dept
ORDER BY ID
zjoxc 2008-12-19
  • 打赏
  • 举报
回复
IF OBJECT_ID('Tempdb..#t') IS NOT NULL
DROP TABLE #t
GO
CREATE TABLE #t([id] INT,[fid] INT)
INSERT #t SELECT 1,0
UNION ALL SELECT 2,1
UNION ALL SELECT 3,2
UNION ALL SELECT 4,2
UNION ALL SELECT 5,4
/*
-- =====================================================
-- 查询指定树路径
-- 邹建(山寨版) 2008-12(引用请保留此信息)
-- =====================================================
*/
GO
;WITH cte (id,p)
AS
(
SELECT id,CAST(id AS VARCHAR(1000)) AS p FROM #t WHERE fid=0
UNION ALL
SELECT b.id, CAST(a.p + ',' + RTRIM(b.id) AS VARCHAR(1000)) FROM cte a ,#t b WHERE a.id=b.fid
)
SELECT * FROM cte

/*
1 1
2 1,2
3 1,2,3
4 1,2,4
5 1,2,4,5
*/
水族杰纶 2008-12-19
  • 打赏
  • 举报
回复
--写自定义函数,查询指定id的所有子結點   
create function f_cid(
@id int
)returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from tb a,@re b
where a.pid=b.id and b.level=@l-1
end
return
end
go
冷箫轻笛 2008-12-19
  • 打赏
  • 举报
回复
......

从今天起,收集乌龟的句子
dawugui 2008-12-19
  • 打赏
  • 举报
回复
/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid

22,207

社区成员

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

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