利用SCOTT.EMP中的empno和mgr的关系做个示例:
使用层次结构查询,一个语句就能查出(EMPNO和MGR关系的)整个树结构
col level for a15
col ename for a10
set pagesize 100
1.语句一
SELECT EMPNO,ENAME,MGR,level
FROM EMP
CONNECT BY prior EMPNO=MGR
START WITH ENAME='KING'
/
---------- -------------------- ---------- ----------
注:这个结构是从左到右的顺序排序
2.语句二
SELECT EMPNO,ENAME,MGR,lpad('|',level*3) "LEVEL"
FROM EMP
CONNECT BY prior EMPNO=MGR
START WITH ENAME='KING'
/
---------- -------------------- ---------- --------------------
3.语句三
SELECT EMPNO,ENAME,MGR,lpad('*',level,'*') "LEVEL"
FROM EMP
CONNECT BY prior EMPNO=MGR
START WITH ENAME='KING'
/
---------- -------------------- ---------- --------------------
4.语句四
除去其中某个节点不显示,如不显示scott单个人
SELECT EMPNO,ENAME,MGR,lpad('|',level*3) "LEVEL"
FROM EMP
where ename!='SCOTT'
CONNECT BY prior EMPNO=MGR
START WITH ENAME='KING'
/
---------- ---------- ---------- ---------------
5.语句五
不显示单个节点及节点以后的分支,以scott为例
SELECT EMPNO,ENAME,MGR,lpad('|',level*3) "LEVEL"
FROM EMP
CONNECT BY prior EMPNO=MGR
and ename!='SCOTT'
START WITH ENAME='KING'
/
---------- ---------- ---------- ---------------