码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-06-01 15:36:54      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:rom   com   select   info   通过   创建   img   http   IV   

 

基础表创建:

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' 树结构查询

标签:rom   com   select   info   通过   创建   img   http   IV   

原文地址:https://www.cnblogs.com/-beauTiFul/p/9121553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!