階層式查詢 Start with & Connect by

Start with & Connect by

USER_TABLE

EMPLYEE_ID SUPERVISOR_ID
001 002
002 003
003  


START WITH emplyee_id = '001'
CONNECT BY emplyee_id = PRIOR supervisor_id

Output:

001

002

003

P.S  CONNECT BY 欄位A = PRIOR 欄位B, 若欄位A與欄位B交換位置, 會變成向下追蹤, 若改成 CONNECT BY supervisor_id = PRIOR emplyee_id, 結果如下:

001

因為 001 沒有部屬

 

DEPT_TABLE

DEPT_ID UPPER_DEPT
A B
B C
C  


START WITH dept_id = 'A'
CONNECT BY dept_id = PRIOR upper_dept

Output:

A

B

C