oracle中start with connect by prior
可以用START WITH...CONNECT BY PRIOR子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本語法是:[@more@]
select ... from tablename start with cond1
connect by cond2
where cond3;
簡單說來是將一個樹狀結構儲存在一張表裡,比如一個表中存在兩個欄位:
id,parentid那麼透過表示每一條記錄的parent是誰,就可以形成一個樹狀結構。
用上述語法的查詢可以取得這棵樹的所有記錄。
其中COND1是根結點的限定語句,當然可以放寬限定條件,以取得多個根結點,實際就是多棵樹。
COND2是連線條件,其中用PRIOR表示上一條記錄,比如 CONNECT BY PRIOR ID=PRAENTID就是說上一條記錄的ID是本條記錄的PRAENTID,即本記錄的父親是上一條記錄。
COND3是過濾條件,用於對返回的所有記錄進行過濾。
connect by後面的prior如果預設,則只能查到符合條件其實行,並不進行遞迴查詢,connect by prior後面
所放的欄位知名了查詢方向。
========================================================
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
14 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
14 rows selected.
透過子節點像根節點追朔
SQL> select empno,ename,mgr from emp start with empno=7698 connect by prior mgr=empno;
EMPNO ENAME MGR
---------- -------------------- ----------
7698 BLAKE 7839
7839 KING
透過根節點詳子節點追朔
SQL> select empno,ename,mgr from emp start with empno=7698 connect by prior empno=mgr;
EMPNO ENAME MGR
---------- -------------------- ----------
7698 BLAKE 7839
7499 ALLEN 7698
7521 WARD 7698
7654 MARTIN 7698
7844 TURNER 7698
7900 JAMES 7698
EMPNO ENAME MGR
---------- -------------------- ----------
7698 BLAKE 7839
7839 KING
透過根節點詳子節點追朔
SQL> select empno,ename,mgr from emp start with empno=7698 connect by prior empno=mgr;
EMPNO ENAME MGR
---------- -------------------- ----------
7698 BLAKE 7839
7499 ALLEN 7698
7521 WARD 7698
7654 MARTIN 7698
7844 TURNER 7698
7900 JAMES 7698
透過level關鍵字查詢所在層次:
SQL> select empno,ename,mgr,level from emp start with empno=7698 connect by prior empno=mgr;
EMPNO ENAME MGR LEVEL
---------- -------------------- ---------- ----------
7698 BLAKE 7839 1
7499 ALLEN 7698 2
7521 WARD 7698 2
7654 MARTIN 7698 2
7844 TURNER 7698 2
7900 JAMES 7698 2
6 rows selected.
SQL> select empno,ename,mgr,level from emp start with empno=7698 connect by prior mgr=empno;
EMPNO ENAME MGR LEVEL
---------- -------------------- ---------- ----------
7698 BLAKE 7839 1
7839 KING 2
EMPNO ENAME MGR LEVEL
---------- -------------------- ---------- ----------
7698 BLAKE 7839 1
7499 ALLEN 7698 2
7521 WARD 7698 2
7654 MARTIN 7698 2
7844 TURNER 7698 2
7900 JAMES 7698 2
6 rows selected.
SQL> select empno,ename,mgr,level from emp start with empno=7698 connect by prior mgr=empno;
EMPNO ENAME MGR LEVEL
---------- -------------------- ---------- ----------
7698 BLAKE 7839 1
7839 KING 2
----------------------------------------------------------------------------------------------------------
connect by後面的prior如果預設,則只能查到符合條件其實行,並不進行遞迴查詢,connect by prior後面
所放的欄位知名了查詢方向。
SQL> select empno,ename,mgr,level from emp start with empno=7698 connect by empno=mgr;
EMPNO ENAME MGR LEVEL
---------- -------------------- ---------- ----------
7698 BLAKE 7839 1
EMPNO ENAME MGR LEVEL
---------- -------------------- ---------- ----------
7698 BLAKE 7839 1
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24237320/viewspace-1060181/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- start with connect by prior
- Oracle中start with...connect by prior子句用法.docOracle
- Oracle 樹操作(select…start with…connect by…prior)Oracle
- start with...connect by prior用法
- connect by ...[prior]...start with 學習
- 請教start with ......connect by prior .....用法
- START WITH...CONNECT BY PRIOR簡單使用
- 樹型結構start with..........connect by prior
- Oracle Connect By Prior 詳解Oracle
- Oracle中 connect by prior 遞迴演算法Oracle遞迴演算法
- 【--SQL基礎-->層次化查詢(START BY ... CONNECT BY PRIOR)】SQL
- Oracle Start with ....Connect ByOracle
- oracle中 connect by prior 遞迴演算法 (轉)Oracle遞迴演算法
- oracle中connect by prior實現遞迴查詢Oracle遞迴
- START WITH and CONNECT BY in Oracle SQLOracleSQL
- Oracle start with connect by PostgreSQL recursive cteOracleSQL
- oracle中的遞迴sql查詢 connect by prior實現遞迴Oracle遞迴SQL
- 樹狀sql中connect by裡prior的含義!SQL
- start with ... connect by
- oracle樹中prior的用法Oracle
- start with connect by 用法
- START WITH...CONNECT BY
- [轉載] Oracle:start with...connect by子句的用法Oracle
- connect by..start with..
- start with ...connect by --轉載
- start with ... connect by用法簡介
- Oracle 樹查詢 效能最佳化紀實(start with, connect by)Oracle
- [轉載]使用Oracle樹形查詢應注意 - Start with / Connect byOracle
- ORACLE 樹形查詢(connect by...start with...)的應用(三)Oracle
- ORACLE 樹形查詢(connect by...start with...)的應用(一)Oracle
- 分析函式connect by | start with學習函式
- [求助] start with connect by 奇怪的問題
- Oracle優化案例-定位start with connect by遞迴死迴圈資料(二十二)Oracle優化遞迴
- Oracle connect byOracle
- linux start oracleLinuxOracle
- Oracle中的Connect、session、process的區別OracleSession
- 【SQL 學習】層次化查詢之CONNECT BY 和 START WITHSQL
- Laravel connect oracleLaravelOracle