一条SQL语句,查出一棵树

zsyok 2011-03-10 11:30:14
id pid name
1 0 a
2 0 b
3 1 c
4 3 d
5 4 e


查出 最后e向上的树怎样搞?
...全文
389 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanquan121 2011-03-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wangchangming 的回复:]
declare @table table (A int,B int)
insert into @table
select 1,0 union all
select 2,1 union all
select 3,1 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5
……
[/Quote].0
神码浮云 2011-03-10
  • 打赏
  • 举报
回复
Select name From TableName Order By id Desc

flyerwing 2011-03-10
  • 打赏
  • 举报
回复
4楼的行,ORACLE里一句就能搞定.
zsyok 2011-03-10
  • 打赏
  • 举报
回复
id pid name
1 0 a
2 0 b
3 1 c
4 3 d
5 4 e

要求 查出来的结果a c d e
真的没看懂吗

子夜__ 2011-03-10
  • 打赏
  • 举报
回复
DEMO

tb_city表结构如下
id name parentid
1 湖北 0
2 湖南 0
3 武汉 1
4 仙桃 1
5 长沙 2
6 蔡甸 3

   1. create function c_tree(@initid int)/*定义函数c_tree,输入参数为初始节点id*/  
2. returns @t table(id int,name varchar(100),parentid int,lev int)/*定义表t用来存放取出的数据*/
3. begin
4. declare @i int/*标志递归级别*/
5. set @i=1
6. insert @t select id,name,parentid,@i from tb_city where id=@initid
7. while @@rowcount<>0
8. begin
9. set @i=@i+1
10. insert @t select a.id,a.name,a.parentid,@i from tb_city as a,@t as b
11. where b.id=a.parentid and b.lev=@i-1
12. end
13. return
14. end



你的
create function c_tree(@initid int)/*定义函数c_tree,输入参数为初始节点id*/  
returns @t table(id int,name varchar(100),parentid int,lev int)/*定义表t用来存放取出的数据*/
begin
declare @i int/*标志递归级别*/
set @i=1
insert @t select id,name,pid ,@i from tb where id=@initid
while @@rowcount<>0
begin
set @i=@i+1
insert @t select a.id,a.name,a.pid ,@i from tb as a,@t as b
where b.id=a.pid and b.lev=@i-1
end
return
end
ycproc 2011-03-10
  • 打赏
  • 举报
回复
游标向下循环查询PID
其实你这少一个字段 不知道你看懂了没
q107770540 2011-03-10
  • 打赏
  • 举报
回复
感慨。。提问真是一门艺术
AstaChen 2011-03-10
  • 打赏
  • 举报
回复
是oracle吗?
select * from tablename start with name='e' connect by prior pid=id
修改一下昵称 2011-03-10
  • 打赏
  • 举报
回复

DECLARE @Table TABLE(Id int,Pid int,Name char)

INSERT INTO @Table
SELECT 1,0,'a' UNION ALL
SELECT 2,0,'b' UNION ALL
SELECT 3,1,'c' UNION ALL
SELECT 4,3,'d' UNION ALL
SELECT 5,4,'e'

;WITH List AS(
SELECT * FROM @Table T WHERE T.Name='a'
UNION ALL
SELECT T2.* FROM List T , @Table T2 WHERE T.Id=T2.Pid
)

SELECT L.* FROM List L

/*
Id Pid Name
----------- ----------- ----
1 0 a
3 1 c
4 3 d
5 4 e

(4 行受影响)
*/

wangchangming 2011-03-10
  • 打赏
  • 举报
回复
declare @table table (A int,B int)
insert into @table
select 1,0 union all
select 2,1 union all
select 3,1 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5

;
WITH Maco
AS ( SELECT A ,
B
FROM @table
WHERE A = 1
UNION ALL
SELECT a.A ,
a.B
FROM @table AS a ,
Maco AS b
WHERE a.B = b.A
)
SELECT *
FROM Maco

zsyok 2011-03-10
  • 打赏
  • 举报
回复
那里有相关的教程。
我见网上的都是教SQL 语句的。

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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