MSSQL 查询所有节点及其所有子节点的函数

*
标题:查询各节点的父路径函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)  
时间:2008-05-12
地点:广东深圳
*/


/*
原始数据及要求结果如下:
--食品 
  --水果 
    --香蕉 
    --苹果    
  --蔬菜 
    --青菜
id          pid         name                 
----------- ----------- -------------------- 
1           0           食品
2           1           水果
3           1           蔬菜
4           2           香蕉
5           2           苹果
6           3           青菜


要求得到各节点的父路径即如下结果:
id  pid name  路径                         
--- --- ----- ---------------
1   0   食品  食品
2   1   水果  食品,水果
3   1   蔬菜  食品,蔬菜
4   2   香蕉  食品,水果,香蕉
5   2   苹果  食品,水果,苹果
6   3   青菜  食品,蔬菜,青菜 
*/


create table tb (id int , pid int , name nvarchar(20)) 
insert into tb values(1 , 0 , '食品')
insert into tb values(2 , 1 , '水果')
insert into tb values(3 , 1 , '蔬菜')
insert into tb values(4 , 2 , '香蕉')
insert into tb values(5 , 2 , '苹果')
insert into tb values(6 , 3 , '青菜')
go


--查询各节点的父路径函数
create function f_pid(@id int) returns varchar(100)
as
begin
  declare @re_str as varchar(100)
  set @re_str = ''
  select @re_str = name from tb where id = @id
  while exists (select 1 from tb where id = @id and pid <> 0)
    begin
      select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
    end
  return @re_str
end
go


select * , dbo.f_pid(id) 路径 from tb order by id


drop table tb
drop function f_pid






SQL code
/*
标题:查询所有节点及其所有子节点的函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
时间:2009-04-12
地点:广东深圳
*/


--生成测试数据 
create table tb(id varchar(10),pid varchar(10)) 
insert into tb select 'a', null 
insert into tb select 'b', 'a' 
insert into tb select 'c', 'a' 
insert into tb select 'd', 'b' 
insert into tb select 'e', 'b' 
insert into tb select 'f', 'c' 
insert into tb select 'g', 'c' 
go 


--创建用户定义函数 
create function f_getchild(@id varchar(10)) returns varchar(8000) 
as 
begin 
  declare @i int , @ret varchar(8000) 
  declare @t table(id varchar(10) , pid varchar(10) , level int) 
  set @i = 1 
  insert into @t select id , pid , @i from tb where id = @id 
  while @@rowcount <> 0 
  begin 
    set @i = @i + 1 
    insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
  end 
  select @ret = isnull(@ret , '') + id + ',' from @t 
  return left(@ret , len(@ret) - 1)
end 
go 


--执行查询 
select id , children = isnull(dbo.f_getchild(id) , '') from tb group by id
go 


--输出结果 
/* 
id         children     
---------- -------------
a          a,b,c,d,e,f,g
b          b,d,e
c          c,f,g
d          d
e          e
f          f
g          g


(所影响的行数为 7 行)


*/ 


--删除测试数据 
drop function f_getchild 
drop table tb






SQL code
/*
标题:查询所有顶级节点及其子节点的例
地址:http://www.52mvc.com
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2009-03-23
地点:广东深圳
*/


[code=SQL]create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0) 
insert into area values('四川省',2,0) 
insert into area values('湖北省',2,0) 
insert into area values('东莞市',1,1) 
insert into area values('广州市',1,1) 
insert into area values('天河区',0,5) 
insert into area values('绵阳市',1,2) 
insert into area values('武汉市',1,3) 
insert into area values('汉口区',0,8) 
insert into area values('随州市',1,3)
go


select * from area


drop table area


/*
id          Name       order_by    father_ID   
----------- ---------- ----------- ----------- 
1           广东省        2           0
2           四川省        2           0
3           湖北省        2           0
4           东莞市        1           1
5           广州市        1           1
6           天河区        0           5
7           绵阳市        1           2
8           武汉市        1           3
9           汉口区        0           8
10          随州市        1           3


(所影响的行数为 10 行)


要求显示为:
name           
-------------- 
广东省
  东莞市
  广州市
    天河区
四川省
  绵阳市
湖北省
  武汉市
    汉口区
  随州市


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




SQL code
--创建原始表
create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0) 
insert into area values('四川省',2,0) 
insert into area values('湖北省',2,0) 
insert into area values('东莞市',1,1) 
insert into area values('广州市',1,1) 
insert into area values('天河区',0,5) 
insert into area values('绵阳市',1,2) 
insert into area values('武汉市',1,3) 
insert into area values('汉口区',0,8) 
insert into area values('随州市',1,3)
--创建临时表
create table tmp (id int identity,Name varchar(10) ,order_by int ,father_ID int )
go


--创建查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(id int , 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 area a , @t_Level b
    where a.father_ID = b.id and b.level = @level - 1
  end
  return
end
go


--创建存储过程并将数据插入临时表
create proc my_proc 
as
begin
  declare @id as int
  set @id = 0
  while exists(select 1 from area where order_by = 2 and id > @id)
  begin
    set @id = (select min(id) from area where order_by = 2 and id > @id)
    insert into tmp(Name ,order_by ,father_ID) select a.name,a.order_by ,a.father_id from area a , f_cid(@id) b where a.id = b.id order by a.id 
  end
end
go
exec my_proc


--从临时表提取数据并显示
select case when order_by = 2 then name
            when order_by = 1 then '  ' + name
            when order_by = 0 then '    ' + name
       end name
from tmp order by id


drop function f_cid
drop proc my_proc
drop table area , tmp


/*
name           
-------------- 
广东省
  东莞市
  广州市
    天河区
四川省
  绵阳市
湖北省
  武汉市
    汉口区
  随州市


(所影响的行数为 10 行)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL中可以使用递归CTE(通用表达式)来创建查询子节点函数。以下是一个简单的例子: 假设我们有一个表格tree,其中包含两个列:id和parent_id。id表示节点的唯一标识符,parent_id表示该节点的父节点的标识符。我们想要编写一个函数,该函数接受一个节点的id作为输入,并返回该节点的所有子节点的id。 首先,我们可以创建一个递归CTE来查找给定节点的所有子节点: WITH RECURSIVE sub_tree AS ( SELECT id, parent_id FROM tree WHERE id = <input_id> UNION ALL SELECT t.id, t.parent_id FROM tree t JOIN sub_tree st ON t.parent_id = st.id ) SELECT id FROM sub_tree; 在上面的查询中,我们首先选择给定的节点,然后使用UNION ALL将其与所有直接子节点组合在一起。然后,在每个递归步骤中,我们选择与先前选择的子节点相关联的所有节点,并将它们添加到结果集中。这样,我们可以使用CTE来获取给定节点的所有子节点。 接下来,我们可以将上述查询封装在一个函数中: DELIMITER // CREATE FUNCTION get_sub_tree(input_id INT) RETURNS VARCHAR(255) BEGIN DECLARE result VARCHAR(255) DEFAULT ''; WITH RECURSIVE sub_tree AS ( SELECT id, parent_id FROM tree WHERE id = input_id UNION ALL SELECT t.id, t.parent_id FROM tree t JOIN sub_tree st ON t.parent_id = st.id ) SELECT GROUP_CONCAT(id) INTO result FROM sub_tree; RETURN result; END // DELIMITER ; 在上面的函数中,我们首先定义一个名为result的变量,该变量将用于存储结果。然后,我们使用上面的递归CTE查询获取给定节点的所有子节点,并使用GROUP_CONCAT将所有子节点的id连接成一个字符串。最后,我们返回该字符串作为函数的结果。 现在,我们可以使用以下方式调用该函数来获取节点1的所有子节点: SELECT get_sub_tree(1); 该函数将返回一个逗号分隔的子节点id列表,如下所示: 2,3,4,5,6 这些就是MySQL创建查询子节点函数的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值