Connect By


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) 查詢一個節點的叔叔伯父節點

--查看emp_id為6的節點的叔叔伯父節點
with temp as (
    select  employee.*,
            prior emp_name,
            level le
    from employee 
    start with lead_id = 0
    connect by lead_id=prior emp_id
)
select * from temp t left join temp tt on tt.emp_id=6 --此處需要限定 where t.le = (tt.le-1) and t.emp_id not in (tt.lead_id)

 

 

 

(4) 查詢族兄

--查看employee id是6的節點的族兄節點
with temp 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 temp  t
left outer join temp 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;

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM