select *from wd_dept connect by prior id=parent_dept_id start with id=‘4028810a6d13c741016d13e9c9410003’;//父取子
select *from wd_dept connect by prior id=parent_dept_id start with parent_dept_id=‘4028810a6d13c741016d13e9c9410003’;
select *from wd_dept connect by prior parent_dept_id=id start with parent_dept_id=‘4028810a6d13c741016d13e9c9410003’;//子取父
关键词prior,prior跟父节点列parentid放在一起,就是往父结点方向遍历;prior跟子结点列subid放在一起
,则往叶子结点方向遍历,parentid、subid两列谁放在“=”前都无所谓,关键是prior跟谁在一起。
SELECT LEVEL,
Wd.Dept_Code,
Wd.Dept_Name,
Wd2.Dept_Name,
Wd.Parent_Dept_Id,
Rownum
FROM Hr.Wd_Dept Wd
LEFT JOIN Hr.Wd_Dept Wd2
ON Wd.Parent_Dept_Id = Wd2.Id
CONNECT BY PRIOR Wd.Id = Wd.Parent_Dept_Id
START WITH Wd.Parent_Dept_Id = '-1'
ORDER BY Rownum;