oracle connect by用法
oracle中的select語句可以用START WITH...CONNECT BY PRIOR子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本語法是:
select ... from
where
start with
connect by
;
舉例
資料組織結構如下圖:
|----a-----a-a
| |--a-b
| |--a-c----a-c-a
|
|
|----b-----b-a
| |--b-b
表結構如下:
CREATE TABLE TREE
(ID NUMBER,
ROOT_ID NUMBER,
NAME VARCHAR2(5 BYTE)
)
ID ROOT_ID NAME 1 0 a 2 1 a-a 3 1 a-b 4 0 b 5 4 b-a 6 4 b-b 7 1 a-c 8 7 a-c-a
獲取完整樹( 從Root往樹末梢遞迴):
select * from TREE
start with root_id = 0
connect by prior id = root_id;
ROOT_ID ID NAME 0 1 a 1 2 a-a 1 3 a-b 1 7 a-c 7 8 a-c-a 0 4 b 4 5 b-a 4 6 b-b
|----a-----a-a
| |--a-b
| |--a-c----a-c-a
|
|
|----b-----b-a
| |--b-b
獲取特定子樹:
select * from TREE
start with id = 1
connect by prior id = root_id;
ROOT_ID ID NAME 0 1 a 1 2 a-a 1 3 a-b 1 7 a-c 7 8 a-c-a |----a-----a-a
| |--a-b
| |--a-c----a-c-a
select * from TREE
start with id = 4
connect by prior id = root_id;
ROOT_ID ID NAME 0 4 b 4 5 b-a 4 6 b-b |----a-----a-a
| |--a-b
| |--a-c----a-c-a
如果connect by prior中的prior被省略,則查詢將不進行深層遞迴。
select * from TREE
start with root_id = 0
connect by id = root_id;
ROOT_ID ID NAME 0 1 a 0 4 b |----a|----b
select * from TREE
start with root_id = 1
connect by id = root_id;
ROOT_ID ID NAME 1 7 a-c 1 2 a-a 1 3 a-b |----a-a
|----a-b
|----a-c
從末梢往樹ROOT遞迴
select * from TREE
start with id=8
connect by prior root_id= id
ID ROOT_ID NAME 8 7 a-c-a 7 1 a-c 1 0 a
a-c-a--------a-c--------a
參考:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23650854/viewspace-693105/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [轉載] Oracle:start with...connect by子句的用法Oracle
- Oracle中start with...connect by prior子句用法.docOracle
- start with connect by 用法
- start with...connect by prior用法
- start with ... connect by用法簡介
- 請教start with ......connect by prior .....用法
- Oracle connect byOracle
- Laravel connect oracleLaravelOracle
- Oracle “CONNECT BY” 使用Oracle
- Oracle Start with ....Connect ByOracle
- START WITH and CONNECT BY in Oracle SQLOracleSQL
- oracle中start with connect by priorOracle
- oracle的connect by語句Oracle
- 【函式】Oracle “CONNECT BY” 使用函式Oracle
- Oracle Connect By Prior 詳解Oracle
- ORACLE WITH AS 用法Oracle
- Linux Use ODBC Connect OracleLinuxOracle
- Oracle start with connect by PostgreSQL recursive cteOracleSQL
- Oracle Connect by 單行變多行Oracle
- Oracle Easy Connect Naming methodOracle
- ORACLE SEQUENCE用法Oracle
- oracle cast 用法OracleAST
- oracle job用法Oracle
- oracle restrice用法OracleREST
- PostgreSQL Oracle 相容性 - connect by 2SQLOracle
- WebMethod connect to Oracle10g RACWebOracle
- Oracle Transparent GateWay connect to MYSQL fix BUGOracleGatewayMySql
- Oracle禁止connect / as sysdba方式登入Oracle
- Unable to Connect to Database with Oracle Client Software for WindowsDatabaseOracleclientWindows
- Oracle EXPLAIN PLAN用法OracleAI
- oracle merge into用法Oracle
- 關於oracle with as用法Oracle
- Oracle sql trace用法OracleSQL
- Oracle Hints的用法Oracle
- oracle job的用法Oracle
- oracle exp imp 用法Oracle
- Oracle keep的用法Oracle
- oracle job 用法2Oracle