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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 從10046看Oracle分割槽裁剪Oracle
- Oracle中的B樹索引Oracle索引
- oracle樹中prior的用法Oracle
- ps裁剪工具怎麼自由裁剪 ps如何裁剪自己想要的圖片尺寸
- oracle樹形查詢Oracle
- 怎麼裁剪圖片?PerfectlyClear Complete裁剪圖片的方法
- Oracle如何建立B樹索引Oracle索引
- 裁剪序列
- Oracle如何管理帶約束的B樹索引Oracle索引
- Oracle如何實現B樹索引Oracle索引
- Flutter——路徑裁剪Flutter
- 裁剪序列Cut the Sequence
- Python批次裁剪圖片Python
- GUI.BeginClip裁剪使用GUI
- 裁剪上傳圖片
- canvas實現的前端壓縮裁剪工具Canvas前端
- ArcGIS批量裁剪遙感影像
- Android 仿微信, QQ 裁剪Android
- octobercms 圖片裁剪外掛
- Linux 核心裁剪框架初探Linux框架
- Java 圖片裁剪,擷取Java
- 如何裁剪AVI檔案呢?
- 基於React Hook實現圖片的裁剪ReactHook
- 大量影片的畫面怎麼批次裁剪大小?
- LightDB 22.4 新特性之相容Oracle樹形查詢Oracle
- oracle之樹狀結構的儲存與展示(遞迴查詢)Oracle遞迴
- WIN10怎麼把音訊裁剪 WIN10系統如何裁剪音訊Win10音訊
- TimThumb——超好用的 PHP 略縮圖裁剪外掛PHP
- Vue-cropper 圖片裁剪的基本原理Vue
- Flutter 裁剪類元件 最全總結Flutter元件
- Vue圖片裁剪上傳元件Vue元件
- windows10怎樣裁剪圖片_Win10如何使用自帶工具裁剪圖片WindowsWin10
- 基於cropper.js的圖片上傳和裁剪JS
- intervention/image 伺服器圖片裁剪伺服器
- 圖片裁剪上傳示例(node + react)React
- python 裁剪圖片;位深度不變Python
- 圖片裁剪-文字識別-文字新增
- iOS自定義拍照框拍照&裁剪(一)iOS