Connect By

connect by 用於存在父子,祖孫,上下級等層級關係的數據表進行層級查詢。spa

 

語法格式:
    { CONNECT BY [ NOCYCLE ] condition [AND condition]... [ START WITH condition ]
    | START WITH condition CONNECT BY [ NOCYCLE ] condition [AND condition]...
    }code

 

特殊詞講解blog

    start with: 指定起始節點的條件it

    connect by: 指定父子行的條件關係io

    prior: 查詢父行的限定符,格式: prior column1 = column2 or column1 = prior column2 and ... ,table

    nocycle: 若數據表中存在循環行,那麼不添加此關鍵字會報錯,添加關鍵字後,便不會報錯,但循環的兩行只會顯示其中的第一條class

    循環行: 該行只有一個子行,並且子行又是該行的祖先行select

    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;

葉節點指的是沒有子節點的節點,那些是既是父節點又是子節點的節點不屬於葉節點

相關文章
相關標籤/搜索