首页 > 数据库技术 > 详细

ORACLE:start with ... connect by prior

时间:2020-05-01 09:56:34      阅读:65      评论:0      收藏:0      [点我收藏+]
--start with ... connect by prior
--case1
select * from org o
--excute order=>first:start with connect by prior, then where condition
where o.flag = 1

--start with subNodId=‘...‘ connect by prior subNodeId = parentNodeId
start with o.org_code=10000
--start with parentNodId=‘...‘ connect by prior subNodeId = parentNodeId
start with o.org_parent_code=10000

connect by prior o.org_code=o.org_parent_code


--case2
select * from org o

--start with subNodeId=‘...‘ connect by subNodeId = prior parentNodeId
start with o.org_code=10000
--start with parentNodeId=‘...‘ connect by subNodeId = prior parentNodeId
start with o.org_parent_code=10000

connect by o.org_code=prior o.org_parent_code

--start with clause: 
--There is a little trick to traverse the starting conditions. 
--If you want to check the parent node, you can use the column of the child node and vice versa.

--connect by clause:
--Connection conditions:The key word priority, put together with the parentid of the parent node column, is to traverse toward the parent node;
--put priority together with the child node column subid, then traverse to the direction of the leaf node,

--to sum up: It doesn‘t matter which one puts in front of "=" to parentid and subid, the key point is who keep together with prior.



--CONNECT_BY_ROOT
--qry average salary for each team
select name, avg(sal)
fom
    (select CONNECT_BY_ROOT t.user_name as name, t.user_salary as salary
     from employee
     start with t.emp_no = 10000
     connet by prior t.emp_no = t.manager_no
    )
group by name

 

ORACLE:start with ... connect by prior

原文:https://www.cnblogs.com/xinyueliu/p/12812370.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!