connect by..start with..
1、格式化層次化查詢的結果:(level偽列,顯示顯示樹中的層次數)
select level,LPAD(' ',5*level-1) || first_name || ' ' || last_name
as employee
from more_employees
start with employee_id=1
connect by prior employee_id=manager_id;
查詢結果:
level employee
-----------------------------------------
1 1 James Smith
2 2 Ron Johnson
3 3 Fred Hobbs
4 3 Rob Green
5 2 Susan Jones
6 3 Jane Brown
7 4 Henry Heyson
8 3 John Grey
9 4 Jean Blue
10 2 Kevin Black
11 3 Keith Long
12 3 Frank Howard
13 3 Doreen Penn
透過檢視格式化之後的結果、員工關係一目瞭然。
2、start with句子中使用子查詢
select level,LPAD(' ',5*level-1) || first_name || ' ' || last_name
as employee
from more_employees
start with employee_id=(
select employee_id from more_employees
where first_name='Susan' and last_name='Jones'
)
connect by prior employee_id=manager_id;
查詢結果:
level employee
-----------------------------
1 Susan Jones
2 Jane Brown
3 Henry Heyson
2 John Grey
3 Jean Blue
3、從下向上遍歷樹
select level,LPAD(' ',5*level-1) || first_name || ' ' || last_name
as employee
from more_employees
start with employee_id=(
select employee_id from more_employees
where first_name='Susan' and last_name='Jones'
)
connect by prior manager_id=employee_id;
查詢結果:
1 1 Susan Jones
2 2 James Smith
4、子查詢中刪除節點和分支
select level,LPAD(' ',5*level-1) || first_name || ' ' || last_name
as employee
from more_employees
where last_name !='Johnson'
start with employee_id=1
connect by prior employee_id=manager_id;
1 1 James Smith
2 3 Fred Hobbs
3 3 Rob Green
4 2 Susan Jones
5 3 Jane Brown
6 4 Henry Heyson
7 3 John Grey
8 4 Jean Blue
9 2 Kevin Black
10 3 Keith Long
11 3 Frank Howard
12 3 Doreen Penn
查詢結果:Ron Johnson已清除,但它的下屬Rob Green 、Fred Hobbs仍存在,
可以再connect by prior使用and子句將 將其Ron Johnson下屬從結果中除去:
select level,LPAD(' ',5*level-1) || first_name || ' ' || last_name
as employee
from more_employees
start with employee_id=1
connect by prior employee_id=manager_id and last_name !='Johnson';
查詢結果:
1 1 James Smith
2 2 Susan Jones
3 3 Jane Brown
4 4 Henry Heyson
5 3 John Grey
6 4 Jean Blue
7 2 Kevin Black
8 3 Keith Long
9 3 Frank Howard
10 3 Doreen Penn
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23135684/viewspace-626702/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle connect byOracle
- start with ... connect by
- Laravel connect oracleLaravelOracle
- database的connectDatabase
- start with connect by prior
- start with connect by 用法
- Oracle “CONNECT BY” 使用Oracle
- session,connect,processSession
- oracle connect by用法Oracle
- Oracle Start with ....Connect ByOracle
- START WITH...CONNECT BY
- connect by層內排序排序
- JDBC connect SCAN IPJDBC
- MySQL-undefinedfunctionmysql_connect()MySqlUndefinedFunction
- START WITH and CONNECT BY in Oracle SQLOracleSQL
- mysql connect speed is slowMySql
- start with ...connect by --轉載
- connect your tunnel to CloudflareCloud
- QObject::connect()函式Object函式
- 【CONNECT BY】使用connect by level/rownum實現連續數字的插入
- PostgreSQLOracle相容性之-connectby高階選項CONNECT_BY_ISLEAF、SYS_CONNECT_BY_PATH、CONNECT_BY_ISCYCLE、LEVELSQLOracle
- 【Qt】connect機制原理QT
- Snapdragon connect to android devicesGoAndroiddev
- About - CONNECT_BY_FILTERINGFilter
- 深入理解connect/expressExpress
- Unable to connect to SQL Server ExpressSQLServerExpress
- Qt Connect 訊號 槽QT
- SAPGUI for Java Connect settingGUIJava
- oracle中start with connect by priorOracle
- start with...connect by prior用法
- connect by ...[prior]...start with 學習
- oracle的connect by語句Oracle
- connect by 的排序問題排序
- 【函式】Oracle “CONNECT BY” 使用函式Oracle
- Oracle Connect By Prior 詳解Oracle
- start with ... connect by用法簡介
- 17、Connect-the-dots(VulnHub)
- INBOUND_CONNECT_TIMEOUT與SQLNET.INBOUND_CONNECT_TIMEOUT小結SQL