connect by prior id= pid start with id='1' 树结构查询

Posted -beautiful

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了connect by prior id= pid start with id='1' 树结构查询相关的知识,希望对你有一定的参考价值。

 

基础表创建:

with temp as (
select 公司 name, 1 id, ‘‘ pid from dual union all
select 部门1  name , 11 id, 1 pid from dual union all
select 部门2  name , 12 id, 1 pid from dual union all
select 员工11 name , 111 id , 11 pid from dual union all
select 员工12 name , 112 id , 11 pid from dual union all
select 员工21 name , 121 id , 12 pid from dual 
)

等号左边的字段为基础,查询右边字段=左边字段的

从上往下查

通过根节点可以获取包括该根节点及以下的所有子节点
select * from temp
connect by prior  id= pid
start with id=1

 技术分享图片

根节点可以多选
select * from temp
connect by prior  id= pid
start with id in (11 ,12)

技术分享图片

添加where语句

select * from temp
where id = 111
connect by prior  id= pid
start with id in (11 ,12)

技术分享图片

 从下往上查

select * from temp
connect by prior pid= id 
start with id = 121

技术分享图片

 

以上是关于connect by prior id= pid start with id='1' 树结构查询的主要内容,如果未能解决你的问题,请参考以下文章

oracle CONNECT BY PRIOR叶子节点查找根节点

Oracle 递归查询 (start with ...connect by ...prior)

oracle的start with connect by prior如何使用

oracle中 connect by prior 递归查询

ORACLE 树结构查询 start with id = 'root_number' connect by prior id = parentId.

Oracle PL/SQL CONNECT BY PRIOR ... SQL Server 中的 START WITH 语句