//如何得到一个树下面的所有叶结点

delwin 2004-05-12 11:30:30
//如何得到一个树下面的所有叶结点
//CID ,PID
//1, 0
//2, 0
//3, 0
//4, 1
//5, 2
//6, 3
...全文
66 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
email0755 2004-05-12
  • 打赏
  • 举报
回复
呵呵!真假李奎现身CSDN?
zjcxc 2004-05-12
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(cid int,pid int)
insert 表 select 1,0
union all select 2,0
union all select 3,0
union all select 4,1
union all select 5,2
union all select 6,3
go

--查询
select * from 表 a where exists(select 1 from 表 where cid=a.pid)
go

--删除测试
drop table 表

/*--测试结果

cid pid
----------- -----------
4 1
5 2
6 3

(所影响的行数为 3 行)
--*/
zjcxc 2004-05-12
  • 打赏
  • 举报
回复
--写错条件,是exists
select * from 表 a where exists(select 1 from 表 where cid=a.pid)
zjcxc 2004-05-12
  • 打赏
  • 举报
回复
--就是说除顶层外的所有结点?

select * from 表 a where not exists(select 1 from 表 where cid=a.pid)
delwin 2004-05-12
  • 打赏
  • 举报
回复
多谢大家,我是想一下子得到4,5,6这些最下面的叶结点啊
hjhing 2004-05-12
  • 打赏
  • 举报
回复
good idea!
zjcxc 2004-05-12
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(cid int,pid int)
insert 表 select 1,0
union all select 2,0
union all select 3,0
union all select 4,1
union all select 5,2
union all select 6,3
go

--创建查询函数
create function f_id(@id int)
returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert into @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert into @re select a.cid,@l
from 表 a join @re b on a.pid=b.id and b.level=@l-1
end
return
end
go

--调用实现查询
select a.*
from 表 a join f_id(1) b on a.cid=b.id
go

--删除测试
drop table 表
drop function f_id

/*--测试结果
cid pid
----------- -----------
1 0
4 1

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

zjcxc 2004-05-12
  • 打赏
  • 举报
回复
--上面的调用少了一个别名

--创建查询函数
create function f_id(@id int)
returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert into @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert into @re select a.cid,@l
from 表 a join @re b on a.pid=b.id and b.level=@l-1
end
return
end
go

--调用实现查询
select a.*
from 表 a join f_id(1) b on a.cid=b.id
zicxc 2004-05-12
  • 打赏
  • 举报
回复
select * from tablename a
where not exists (
select 1 from tablename where pid=a.cid
)


整个树底下,不用函数
zjcxc 2004-05-12
  • 打赏
  • 举报
回复
create function f_id(@id int)
returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert into @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert into @re select a.cid,@l
from 表 a join @re b on a.pid=b.id and b.level=@l-1
end
return
end
go

--调用实现查询
select a.*
from 表 join f_id(1) b on a.cid=b.id
zjcxc 2004-05-12
  • 打赏
  • 举报
回复
--你是说第一条记录没有name?

insert into @Rt select @pid,@name,@l

改为:
create function Get_Cid(@pid int)
returns @RT Table(id int ,name varchar(20),level int)
as
begin
declare @l int
set @l=0
insert into @Rt select cid,name,@l
from table
where cid=@pid --如果不要第一条记录,则改为: where pid=@pid
while @@rowcount>0
begin
set @l=@l+1
insert into @rt select a.cid,a.name,@L
FROM table A JOIN @RT b ON A.pid=B.id and b.level=@l-1
end
return
end
delwin 2004-05-12
  • 打赏
  • 举报
回复
create function Get_Cid(@pid int)
returns @RT Table(id int ,name varchar(20),level int)
as
begin
declare @l int
declare @name varchar(20)
set @l=0
set @name=''
insert into @Rt select @pid,@name,@l
while @@rowcount>0
begin
set @l=@l+1
insert into @rt select a.cid,a.name,@L
FROM table A JOIN @RT b ON A.pid=B.id and b.level=@l-1
end
return
end

table 结构
cid ,pid,name

select * from get_cid(1)

id name level
----------- -------------------- -----------
1 0
4 食品 1
35 调料 2
36 冻品 2
37 干货 2
38 鲜货 2
39 烟酒饮料小食 2
111 OK汁 3
112 S`S`生油 3
113 八角粉 3
114 白醋 3




怎么结果不对啊?

22,209

社区成员

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

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