树(tree)结构表递归查询的实现方法总结

本文介绍了如何在MySQL中实现树形结构的递归查询,包括从根节点到叶节点以及从叶节点到根节点的遍历。通过示例数据和`START WITH...CONNECT BY`子句来展示具体的查询方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表recursion数据如下:
id 

          name            parentid
          食品分类        -1
          肉类                          1
          蔬菜类                  1
          产品分类        -1
          保健品                  4
          医药                          4
          建筑                          4
一 ORACLE中实现方法:
Oracle中直接支持,使用语句select * from tablename start with connect by
prior id(子层的列)=parentid(属于顶层的列)
语句说明:
start with 指定层次开始的条件,即满足这个条件的行即可以作为层次树的最顶层
connect by prior指层之间的关联条件,即什么样的行是上层行的子行(自连接条件)
实例:
select * from recursionstart with connect by prior>查询结果:
id            name            parentid
          食品分类        -1
          肉类                          1
          蔬菜类                  1
二 MSSQL中的实现方法
在MSSQL中需要使用临时表和循环多次查询的方式实现.
创建函数:
create function GetRecursion(@id int)
returns @t table(
      idint,
      namevarchar(50),
      parentidint
)
as
begin
      insert @tselect * from recursion where>      while @@rowcount>0
              insert @t select a.* from recursion as a inner join @t as b
              on a.parentid=b.id and a.id not in(select id from @t)
return
end
使用方法:
select * from GetRecursion(4)
查询结果:
id            name            parentid
          产品分类        -1
          保健品                  4
          医药                          4
          建筑                          4
三 MYSQL中的实现方法
查询语句:
select b.id,b.name,b.parentid from recursion as a, recursion as bwhere
a.id=b.parentid and (a.id=1 or a. parentid =1)
查询结果:
id            name            parentid
          肉类                        1
          蔬菜类                1
四 在ORACLE、MSSQL、MYSQL中可以使用下面的查询语句只返回树结构表的子结点数据
select *
  from tablename t
  where not exists (select 'X'
                  from tablename t1, tablename t2
                where t1.id = t2.parentid
                    and t1.id = t.id)
如:
select *
  from recursion t
  where not exists (select 'X'
                  from recursion t1, recursion t2
                where t1.id = t2.parentid
                    and t1.id = t.id)
查询结果:
id            name            parentid
          肉类                        1
          蔬菜类                1
          保健品                4
          医药                        4
          建筑                        4
五 在ORACLE、MSSQL、MYSQL中可以使用下面的查询语句只返回树结构表的根结点数据
select *
  from tablename t
  where not exists (select 'X'
                  from tablename t1, tablename t2
                where t1.id = t2.parentid
                    and t1.id = t. parentid)
如:
select *
  from recursion t
  where not exists (select 'X'
                  from recursion t1, recursion t2
                where t1.id = t2.parentid
                    and t1.id = t. parentid)
查询结果:
id            name            parentid
          食品分类      -1
          产品分类      -1



mysql递归查询树形的叶子

  
SELECT
t.name,
t.unit_id,
t.parent_id
FROM
`scpi_unit_struct` t
WHERE NOT EXISTS(
select * from `scpi_unit_struct` t1,`scpi_unit_struct`t2 
where
t1.unit_id=t2.parent_id AND t.unit_id=t1.unit_id
)


 Start with...Connect By子句递归查询

下面是从网上转载的内容:

CREATE

TABLE
TBL_TEST

(

ID   
 NUMBER,

NAME

VARCHAR2(100 BYTE),

PID  
 NUMBER                                  DEFAULT

0

);

 

#插入测试数据:

INSERT

INTO
TBL_TEST(ID,NAME,PID)
VALUES(''''1'''',''''10'''',''''0'''');

INSERT

INTO
TBL_TEST(ID,NAME,PID)
VALUES(''''2'''',''''11'''',''''1'''');

INSERT

INTO
TBL_TEST(ID,NAME,PID)
VALUES(''''3'''',''''20'''',''''0'''');

INSERT

INTO
TBL_TEST(ID,NAME,PID)
VALUES(''''4'''',''''12'''',''''1'''');

INSERT

INTO
TBL_TEST(ID,NAME,PID)
VALUES(''''5'''',''''121'''',''''2'''');

 

#从Root往树末梢递归

select

* from

TBL_TEST

start
with

id=1

connect

by
prior
id = pid

 

#从末梢往树ROOT递归

select

* from

TBL_TEST

start
with

id=5

connect

by
prior
pid = id


MySQL 实现树形的遍历(关于多级菜单栏以及多级上下部门的查询问题)

http://blog.youkuaiyun.com/mchdba/article/details/39277301




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值