本博客是自己在學習和工作途中的積累與總結,僅供自己參考,也歡迎大家轉載,轉載時請注明出處。
http://www.cnblogs.com/king-xg/p/6794562.html
如果覺得對您有幫助,請點擊推薦或收藏本博客,謝謝。
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) 查詢以emp_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 employee where dept_no='002' start with lead_id=1 connect by prior emp_id = lead_id;
(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;
(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;