加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

整理oracle 树形查询

发布时间:2020-12-12 14:12:38 所属栏目:百科 来源:网络整理
导读:http://blog.csdn.net/wupd2014/article/details/52502806 sql树形递归查询是数据库查询的一种特殊情形,也是组织结构、行政区划查询的一种最常用的的情形之一。下面对该种查询进行一些总结: 1、查找某个节点下面子孙节点 select * from table start with i

http://blog.csdn.net/wupd2014/article/details/52502806

sql树形递归查询是数据库查询的一种特殊情形,也是组织结构、行政区划查询的一种最常用的的情形之一。下面对该种查询进行一些总结:

1、查找某个节点下面子孙节点

select * from table start with id=1 connect by priot id=pid
  • 1

2、查找节点的祖先节点

select * from table start with id=5 connect by prior pid=id
  • 1

在树形结构节点很多的情况一下,一般会采用异步刷新的方式进行,在默认加载的情况下,会展开到某个层级。这种情况下,不但要获取某个节点的祖先节点,还需要获取祖先节点的兄弟节点,在这种情况下可以通过level进行

select id,pid,level from tb where level>1
connect by prior id=pid
start with id=1
order by level
  • 1
  • 2
  • 3
  • 4

显示出树的级别查询

select menu_id,rpad(' ',(level-1)*4)||menu_name,level from menu   
 connect by parent_id = prior menu_id   
 start with parent_id is null;
  • 1
  • 2
  • 3

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读