当前位置:   article > 正文

oracle之connect by的用法_oracle connect by用法

oracle 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为层级最高节点

  自定义数据:

复制代码

  1. -- 创建表
  2. create table employee(
  3. emp_id number(18),
  4. lead_id number(18),
  5. emp_name varchar2(200),
  6. salary number(10,2),
  7. dept_no varchar2(8)
  8. );
  9. -- 添加数据
  10. insert into employee values('1',0,'king','1000000.00','001');
  11. insert into employee values('2',1,'jack','50500.00','002');
  12. insert into employee values('3',1,'arise','60000.00','003');
  13. insert into employee values('4',2,'scott','30000.00','002');
  14. insert into employee values('5',2,'tiger','25000.00','002');
  15. insert into employee values('6',3,'wudde','23000.00','003');
  16. insert into employee values('7',3,'joker','21000.00','003');commit;

复制代码

 

  数据列表展示:

  

  数据树形展示:

  

  (1) 查询以emp_id为0开始的节点的所有直属节点

复制代码

  1.   select emp_id,lead_id,emp_name,prior emp_name as lead_name,salary
  2. from employee
  3. start with lead_id=0
  4. connect by prior emp_id = lead_id
  5.   -- 等同于
  6.    select emp_id,lead_id,emp_name,prior emp_name as lead_name,salary
  7.      from employee
  8.      start with  emp_id=1
  9.      connect by prior emp_id =  lead_id

复制代码

  

  (2) 以emp_id为6的所有祖先节点

  1. select emp_id,lead_id,emp_name,salary
  2. from employee
  3. start with emp_id=6
  4. connect by prior lead_id=emp_id;

  (3) 查询一个节点的叔叔伯父节点

复制代码

  1. with t as (
  2. select employee.*,prior emp_name,level le
  3. from employee
  4. start with lead_id = 0
  5. connect by lead_id=prior emp_id
  6. )
  7. select *
  8. from t
  9. left join t tt on tt.emp_id=6
  10. where t.le = (tt.le-1)
  11. and t.emp_id not in (tt.lead_id)

复制代码

  (4) 查询族兄

复制代码

  1. with t as (
  2. select employee.*,prior emp_name,level le
  3. from employee
  4. start with lead_id=0
  5. connect by lead_id= prior emp_id
  6. )
  7. select t.*
  8. from t t
  9. left join t tt on tt.emp_id=6
  10. where t.le=tt.le and t.emp_id<>6;

复制代码

  (5) level伪列的使用,格式化层级

  1. select lpad(' ',level*2,' ')||emp_name as name,emp_id,lead_id,salary,level
  2. from employee
  3. start with lead_id=0
  4. connect by prior emp_id=lead_id

  level数值越低级别越高

 

  (6) connect_by_root 查找根节点

  1. select connect_by_root emp_name,emp_name,lead_id,salary
  2. from employee
  3. where dept_no='002'
  4. start with lead_id=1
  5. connect by prior emp_id = lead_id;

  (7) 标注循环行

复制代码

  1. -- 插入一条数据,与另一条emp_id=7的数据组成循环行
  2. insert into employee values('3',7,'joker_cycle','21000.00','003');
  3. commit;
  4. -- connect_by_iscycle("CYCLE"), connect by nocycle
  5. select emp_id,emp_name,lead_id,salary,connect_by_iscycle as cycle
  6. from employee
  7. start with lead_id=0
  8. connect by nocycle prior emp_id = lead_id;

复制代码

 

  (8) connect_by_isleaf 是否是叶子节点

  1. select emp_id,emp_name,lead_id,salary,connect_by_isleaf
  2. from employee
  3. start with lead_id=0
  4. connect by nocycle prior emp_id=lead_id;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/692416
推荐阅读
相关标签
  

闽ICP备14008679号