Connect by實現樹查詢的妙用

oracle_ace發表於2008-01-09

做了個實驗總結一下connect by樹查詢的用法:

SQL> select * from t_dept_temp;

   DEPT_ID  PARENT_ID DEPT_NAME      AMOUNT
---------- ---------- ---------- ----------
         1            1                   2
         2          1 1-2                15
         3          1 1-3                 8
         4          2 1-2-4              10
         5          2 1-2-5               9
         6          3 1-3-6              17
         7          3 1-3-7               5
         8          3 1-3-8               6

已選擇8行。

SQL> desc t_dept_temp;
 名稱                                      是否為空? 型別
 ----------------------------------------- -------- ----------------------------
 DEPT_ID                                   NOT NULL NUMBER(2)
 PARENT_ID                                          NUMBER(2)
 DEPT_NAME                                          VARCHAR2(10)
 AMOUNT                                             NUMBER(3)

接下來實現各種查詢的測試:
1.查詢dept_id=2以及所有下級部門:
SQL> select level treelevel,dept_id,parent_id,dept_name from      t_dept_temp
  2  start with dept_id=2
  3  connect by parent_id = prior dept_id;

 TREELEVEL    DEPT_ID  PARENT_ID DEPT_NAME
---------- ---------- ---------- ----------
         1          2          1 1-2
         2          4          2 1-2-4
         2          5          2 1-2-5

2.查詢dept_id=4以及所有上級部門:
SQL> select level treelevel,dept_id,parent_id,dept_name from t_dept_temp
  2  start with dept_id=4
  3  connect by dept_id = prior parent_id;

 TREELEVEL    DEPT_ID  PARENT_ID DEPT_NAME
---------- ---------- ---------- ----------
         1          4          2 1-2-4
         2          2          1 1-2
         3          1            1

3.查詢dept_id=1以及所有下級部門:
SQL> select level treelevel,dept_id,parent_id,dept_name from t_dept_temp
  2  start with dept_id=1
  3  connect by parent_id = prior dept_id;

 TREELEVEL    DEPT_ID  PARENT_ID DEPT_NAME
---------- ---------- ---------- ----------
         1          1            1
         2          2          1 1-2
         3          4          2 1-2-4
         3          5          2 1-2-5
         2          3          1 1-3
         3          6          3 1-3-6
         3          7          3 1-3-7
         3          8          3 1-3-8

已選擇8行。

4.查詢dept_id=1以及所有下級部門,但除了部門3 (排除樹枝,部門3下的所有節點也都排除)
SQL> select level treelevel,dept_id,parent_id,dept_name from t_dept_temp
  2  start with dept_id=1
  3  connect by parent_id=prior dept_id and dept_id <> 3;

 TREELEVEL    DEPT_ID  PARENT_ID DEPT_NAME
---------- ---------- ---------- ----------
         1          1            1
         2          2          1 1-2
         3          4          2 1-2-4
         3          5          2 1-2-5

5.查詢dept_id=1以及所有下級部門,但除了部門3 (僅排除部門3,其下的所有節點都是包括的)
SQL> select level treeview, dept_id,parent_id,dept_name from t_dept_temp
  2  where dept_id <> 3
  3  start with dept_id=1
  4  connect by parent_id = prior dept_id;

  TREEVIEW    DEPT_ID  PARENT_ID DEPT_NAME
---------- ---------- ---------- ----------
         1          1            1
         2          2          1 1-2
         3          4          2 1-2-4
         3          5          2 1-2-5
         3          6          3 1-3-6
         3          7          3 1-3-7
         3          8          3 1-3-8

已選擇7行。
注意這裡的where dept_id <> 3,他的執行是在connect by之後做的

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12361284/viewspace-129059/,如需轉載,請註明出處,否則將追究法律責任。

相關文章