oracle樹的裁剪
我們知道用start with ... connect by ...可以生成樹。有時候我們需要對生成的樹進行裁剪,得到我們真正需要的資料。
對樹的裁剪分為去除節點和去除分支兩種,它們的差別就是把條件放在不同的位置。
請看例子:
原樹:
scott@ORA9I> l
1 SELECT LEVEL,RPAD('................',LEVEL*3) ||ENAME EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4* START WITH ENAME='KING'
scott@ORA9I> /
LEVEL EMPLOYEE
---------- ------------------------------------------------------------
1 ...KING
2 ......JONES
3 .........SCOTT
4 ............ADAMS
3 .........FORD
4 ............SMITH
2 ......BLAKE
3 .........ALLEN
3 .........WARD
3 .........MARTIN
3 .........TURNER
3 .........JAMES
2 ......CLARK
3 .........MILLER
scott@ORA9I> l
1 SELECT substr(sys_connect_by_path(ENAME,'->'),3) EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4* START WITH ENAME='KING'
scott@ORA9I> /
EMPLOYEE
------------------------------------------------------------
KING
KING->JONES
KING->JONES->SCOTT
KING->JONES->SCOTT->ADAMS
KING->JONES->FORD
KING->JONES->FORD->SMITH
KING->BLAKE
KING->BLAKE->ALLEN
KING->BLAKE->WARD
KING->BLAKE->MARTIN
KING->BLAKE->TURNER
KING->BLAKE->JAMES
KING->CLARK
KING->CLARK->MILLER
14 rows selected.
去除節點:
scott@ORA9I> l
1 SELECT LEVEL,RPAD('................',LEVEL*3) ||ENAME EMPLOYEE
2 FROM EMP
3 WHERE ENAME!='SCOTT'
4 CONNECT BY PRIOR EMPNO=MGR
5* START WITH ENAME='KING'
scott@ORA9I> /
LEVEL EMPLOYEE
---------- ------------------------------------------------------------
1 ...KING
2 ......JONES
4 ............ADAMS
3 .........FORD
4 ............SMITH
2 ......BLAKE
3 .........ALLEN
3 .........WARD
3 .........MARTIN
3 .........TURNER
3 .........JAMES
2 ......CLARK
3 .........MILLER
13 rows selected.
--在此種表示方式中,ENAME='SCOTT'的節點已經不存在了,但SCOTT所在分支仍然存在,且所在分支的其他節點的LEVEL並沒有改變,如ADAMS的LEVEL在去除節點前後的LEVEL都是4.
scott@ORA9I> l
1 SELECT substr(sys_connect_by_path(ENAME,'->'),3) EMPLOYEE
2 FROM EMP
3 WHERE ENAME!='SCOTT'
4 CONNECT BY PRIOR EMPNO=MGR
5* START WITH ENAME='KING'
scott@ORA9I> /
EMPLOYEE
------------------------------------------------------------
KING
KING->JONES
KING->JONES->SCOTT->ADAMS
KING->JONES->FORD
KING->JONES->FORD->SMITH
KING->BLAKE
KING->BLAKE->ALLEN
KING->BLAKE->WARD
KING->BLAKE->MARTIN
KING->BLAKE->TURNER
KING->BLAKE->JAMES
KING->CLARK
KING->CLARK->MILLER
13 rows selected.
--用這種表示方法,咋一看,好像SCOTT節點仍然存在。其實不是,用這種表示方法,只需要關注每一行的最後部分(也就是節點),可以看出SCOTT節點也是被去除的。
--第三行之所以還出現了SCOTT是因為ADAMS的LEVEL是4,而sys_connect_by_path需要把分支完全顯示。
去除分支:
scott@ORA9I> l
1 SELECT LEVEL,RPAD('................',LEVEL*3) ||ENAME EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4 AND ENAME!='SCOTT'
5* START WITH ENAME='KING'
scott@ORA9I> /
LEVEL EMPLOYEE
---------- ------------------------------------------------------------
1 ...KING
2 ......JONES
3 .........FORD
4 ............SMITH
2 ......BLAKE
3 .........ALLEN
3 .........WARD
3 .........MARTIN
3 .........TURNER
3 .........JAMES
2 ......CLARK
3 .........MILLER
12 rows selected.
--可以看出,SCOTT節點已經不存在,以SCOTT為父節點的節點也不存在,也就是說,SCOTT所在分支已經被去除。
scott@ORA9I> l
1 SELECT substr(sys_connect_by_path(ENAME,'->'),3) EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4 AND ENAME!='SCOTT'
5* START WITH ENAME='KING'
scott@ORA9I> /
EMPLOYEE
------------------------------------------------------------
KING
KING->JONES
KING->JONES->FORD
KING->JONES->FORD->SMITH
KING->BLAKE
KING->BLAKE->ALLEN
KING->BLAKE->WARD
KING->BLAKE->MARTIN
KING->BLAKE->TURNER
KING->BLAKE->JAMES
KING->CLARK
KING->CLARK->MILLER
12 rows selected.
--用這個表示法更明顯地說明SCOTT所在分支已經被去除。
對樹的裁剪分為去除節點和去除分支兩種,它們的差別就是把條件放在不同的位置。
請看例子:
原樹:
scott@ORA9I> l
1 SELECT LEVEL,RPAD('................',LEVEL*3) ||ENAME EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4* START WITH ENAME='KING'
scott@ORA9I> /
LEVEL EMPLOYEE
---------- ------------------------------------------------------------
1 ...KING
2 ......JONES
3 .........SCOTT
4 ............ADAMS
3 .........FORD
4 ............SMITH
2 ......BLAKE
3 .........ALLEN
3 .........WARD
3 .........MARTIN
3 .........TURNER
3 .........JAMES
2 ......CLARK
3 .........MILLER
scott@ORA9I> l
1 SELECT substr(sys_connect_by_path(ENAME,'->'),3) EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4* START WITH ENAME='KING'
scott@ORA9I> /
EMPLOYEE
------------------------------------------------------------
KING
KING->JONES
KING->JONES->SCOTT
KING->JONES->SCOTT->ADAMS
KING->JONES->FORD
KING->JONES->FORD->SMITH
KING->BLAKE
KING->BLAKE->ALLEN
KING->BLAKE->WARD
KING->BLAKE->MARTIN
KING->BLAKE->TURNER
KING->BLAKE->JAMES
KING->CLARK
KING->CLARK->MILLER
14 rows selected.
去除節點:
scott@ORA9I> l
1 SELECT LEVEL,RPAD('................',LEVEL*3) ||ENAME EMPLOYEE
2 FROM EMP
3 WHERE ENAME!='SCOTT'
4 CONNECT BY PRIOR EMPNO=MGR
5* START WITH ENAME='KING'
scott@ORA9I> /
LEVEL EMPLOYEE
---------- ------------------------------------------------------------
1 ...KING
2 ......JONES
4 ............ADAMS
3 .........FORD
4 ............SMITH
2 ......BLAKE
3 .........ALLEN
3 .........WARD
3 .........MARTIN
3 .........TURNER
3 .........JAMES
2 ......CLARK
3 .........MILLER
13 rows selected.
--在此種表示方式中,ENAME='SCOTT'的節點已經不存在了,但SCOTT所在分支仍然存在,且所在分支的其他節點的LEVEL並沒有改變,如ADAMS的LEVEL在去除節點前後的LEVEL都是4.
scott@ORA9I> l
1 SELECT substr(sys_connect_by_path(ENAME,'->'),3) EMPLOYEE
2 FROM EMP
3 WHERE ENAME!='SCOTT'
4 CONNECT BY PRIOR EMPNO=MGR
5* START WITH ENAME='KING'
scott@ORA9I> /
EMPLOYEE
------------------------------------------------------------
KING
KING->JONES
KING->JONES->SCOTT->ADAMS
KING->JONES->FORD
KING->JONES->FORD->SMITH
KING->BLAKE
KING->BLAKE->ALLEN
KING->BLAKE->WARD
KING->BLAKE->MARTIN
KING->BLAKE->TURNER
KING->BLAKE->JAMES
KING->CLARK
KING->CLARK->MILLER
13 rows selected.
--用這種表示方法,咋一看,好像SCOTT節點仍然存在。其實不是,用這種表示方法,只需要關注每一行的最後部分(也就是節點),可以看出SCOTT節點也是被去除的。
--第三行之所以還出現了SCOTT是因為ADAMS的LEVEL是4,而sys_connect_by_path需要把分支完全顯示。
去除分支:
scott@ORA9I> l
1 SELECT LEVEL,RPAD('................',LEVEL*3) ||ENAME EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4 AND ENAME!='SCOTT'
5* START WITH ENAME='KING'
scott@ORA9I> /
LEVEL EMPLOYEE
---------- ------------------------------------------------------------
1 ...KING
2 ......JONES
3 .........FORD
4 ............SMITH
2 ......BLAKE
3 .........ALLEN
3 .........WARD
3 .........MARTIN
3 .........TURNER
3 .........JAMES
2 ......CLARK
3 .........MILLER
12 rows selected.
--可以看出,SCOTT節點已經不存在,以SCOTT為父節點的節點也不存在,也就是說,SCOTT所在分支已經被去除。
scott@ORA9I> l
1 SELECT substr(sys_connect_by_path(ENAME,'->'),3) EMPLOYEE
2 FROM EMP
3 CONNECT BY PRIOR EMPNO=MGR
4 AND ENAME!='SCOTT'
5* START WITH ENAME='KING'
scott@ORA9I> /
EMPLOYEE
------------------------------------------------------------
KING
KING->JONES
KING->JONES->FORD
KING->JONES->FORD->SMITH
KING->BLAKE
KING->BLAKE->ALLEN
KING->BLAKE->WARD
KING->BLAKE->MARTIN
KING->BLAKE->TURNER
KING->BLAKE->JAMES
KING->CLARK
KING->CLARK->MILLER
12 rows selected.
--用這個表示法更明顯地說明SCOTT所在分支已經被去除。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/231499/viewspace-63789/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ps裁剪工具怎麼自由裁剪 ps如何裁剪自己想要的圖片尺寸
- 從10046看Oracle分割槽裁剪Oracle
- 裁剪序列
- oracle樹中prior的用法Oracle
- Oracle中的B樹索引Oracle索引
- Oracle樹 sqlOracleSQL
- oracle 樹查詢Oracle
- Flutter——路徑裁剪Flutter
- ios裁剪圖片iOS
- GPUImage之裁剪篇GPUUI
- 裁剪序列Cut the Sequence
- Oracle中顯示阻塞樹的SQLOracleSQL
- oracle樹形查詢Oracle
- Android核心的編譯與裁剪Android編譯
- ios Image裁剪成圓形的方法iOS
- 【PL/SQL】Oracle--樹的使用(Connect By)SQLOracle
- GUI.BeginClip裁剪使用GUI
- Oracle如何建立B樹索引Oracle索引
- canvas實現的前端壓縮裁剪工具Canvas前端
- Oracle如何管理帶約束的B樹索引Oracle索引
- Oracle 樹形結構查詢的特殊用法Oracle
- ORACLE START WITH 語句的樹級結構例子Oracle
- Android 仿微信, QQ 裁剪Android
- 如何裁剪AVI檔案呢?
- ArcGIS批量裁剪遙感影像
- octobercms 圖片裁剪外掛
- Java 圖片裁剪,擷取Java
- Web端裁剪圖片方法Web
- JAVA實現圖片裁剪Java
- Python批次裁剪圖片Python
- 基於React Hook實現圖片的裁剪ReactHook
- 5 款最新的 jQuery 圖片裁剪外掛jQuery
- Linux核心裁剪的具體步驟Linux
- Oracle如何實現B樹索引Oracle索引
- oracle樹形選單查詢Oracle
- WIN10怎麼把音訊裁剪 WIN10系統如何裁剪音訊Win10音訊
- Flutter 裁剪類元件 最全總結Flutter元件
- Vue圖片裁剪上傳元件Vue元件