connect by 用於存在父子,祖孫,上下級等層級關係的數據表進行層級查詢。 語法格式: { CONNECT BY [ NOCYCLE ] condition [AND condition]... [ START WITH condition ] | START WITH condition C ...
connect by 用於存在父子,祖孫,上下級等層級關係的數據表進行層級查詢。
語法格式:
{ CONNECT BY [ NOCYCLE ] condition [AND condition]... [ START WITH condition ]
| START WITH condition CONNECT BY [ NOCYCLE ] condition [AND condition]...
}
特殊詞講解:
start with: 指定起始節點的條件
connect by: 指定父子行的條件關係
prior: 查詢父行的限定符,格式: prior column1 = column2 or column1 = prior column2 and ... ,
nocycle: 若數據表中存在迴圈行,那麼不添加此關鍵字會報錯,添加關鍵字後,便不會報錯,但迴圈的兩行只會顯示其中的第一條
迴圈行: 該行只有一個子行,而且子行又是該行的祖先行
connect_by_iscycle: 前置條件:在使用了nocycle之後才能使用此關鍵字,用於表示是否是迴圈行,0表示否,1 表示是
connect_by_isleaf: 是否是葉子節點,0表示否,1 表示是
level: level偽列,表示層級,值越小層級越高,level=1為層級最高節點
-- 創建表 create table employee( emp_id number(18), lead_id number(18), emp_name varchar2(200), salary number(10,2), dept_no varchar2(8) ); -- 添加數據 insert into employee values('1',0,'king','1000000.00','001'); insert into employee values('2',1,'jack','50500.00','002'); insert into employee values('3',1,'arise','60000.00','003'); insert into employee values('4',2,'scott','30000.00','002'); insert into employee values('5',2,'tiger','25000.00','002'); insert into employee values('6',3,'wudde','23000.00','003'); insert into employee values('7',3,'joker','21000.00','003'); commit;
(1) 查詢以lead_id為0開始的節點的所有直屬節點
select emp_id,lead_id,emp_name,prior emp_name as lead_name,salary from employee start with lead_id=0 connect by prior emp_id = lead_id -- 等同於 select emp_id,lead_id,emp_name,prior emp_name as lead_name,salary from employee start with emp_id=1 connect by prior emp_id = lead_id
(2) 以emp_id為6的所有祖先節點
select emp_id,lead_id,emp_name,salary from employee start with emp_id=6 connect by prior lead_id=emp_id;
(3) 查詢一個節點的叔叔伯父節點
with t as ( select employee.*,prior emp_name,level le from employee start with lead_id = 0 connect by lead_id=prior emp_id ) select * from t left join t tt on tt.emp_id=6 where t.le = (tt.le-1) and t.emp_id not in (tt.lead_id)
(4) 查詢族兄
with t as ( select employee.*,prior emp_name,level le from employee start with lead_id=0 connect by lead_id= prior emp_id ) select t.* from t t left join t tt on tt.emp_id=6 where t.le=tt.le and t.emp_id<>6;
(5) level偽列的使用,格式化層級
select lpad(' ',level*2,' ')||emp_name as name,emp_id,lead_id,salary,level from employee start with lead_id=0 connect by prior emp_id=lead_id
level數值越低級別越高
(6) connect_by_root 查找根節點
select connect_by_root emp_name,emp_name,lead_id,salary from employe start with lead_id=1 connect by prior emp_id = lead_id;
註意: connect_by_root關鍵字後面跟著欄位,表示根節點對應記錄的某一欄位的值,
如 connect_by_root emp_name表示根節點的員工名,connect_by_root salary表示根節點的工資
(7) 標註迴圈行
-- 插入一條數據,與另一條emp_id=7的數據組成迴圈行 insert into employee values('3',7,'joker_cycle','21000.00','003'); commit; -- connect_by_iscycle("CYCLE"), connect by nocycle select emp_id,emp_name,lead_id,salary,connect_by_iscycle as cycle from employee start with lead_id=0 connect by nocycle prior emp_id = lead_id;
最後一行與新追加的一行是迴圈關係,因此connect_by_iscycle列顯示值為1,但結果集只顯示迴圈的第一條,與之迴圈的另外一條(新追加的一條)是不顯示的
connect by 後面的nocycle關鍵字是防止出現父子關係迴圈的,如果表中出現父子關係迴圈且沒有使用該關鍵字,會報如下錯誤:
--ORA-01436: 用戶數據中的 CONNECT BY 迴圈
(8) connect_by_isleaf 是否是葉子節點
select emp_id,emp_name,lead_id,salary,connect_by_isleaf from employee start with lead_id=0 connect by nocycle prior emp_id=lead_id;
葉節點指的是沒有子節點的節點,那些是既是父節點又是子節點的節點不屬於葉節點