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('',0,'king','1000000.00','');
insert into employee values('',1,'jack','50500.00','');
insert into employee values('',1,'arise','60000.00','');
insert into employee values('',2,'scott','30000.00','');
insert into employee values('',2,'tiger','25000.00','');
insert into employee values('',3,'wudde','23000.00','');
insert into employee values('',3,'joker','21000.00','');
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('',7,'joker_cycle','21000.00','');
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;

叶节点指的是没有子节点的节点,那些是既是父节点又是子节点的节点不属于叶节点

Connect By的更多相关文章

  1. Connect() 2016 大会的主题 ---微软大法好

    文章首发于微信公众号"dotnet跨平台",欢迎关注,可以扫页面左面的二维码. 今年 Connect 大会的主题是 Big possibilities. Bold technolo ...

  2. IdentityServer4 使用OpenID Connect添加用户身份验证

    使用IdentityServer4 实现OpenID Connect服务端,添加用户身份验证.客户端调用,实现授权. IdentityServer4 目前已更新至1.0 版,在之前的文章中有所介绍.I ...

  3. 2003-Can't connect to mysql server on localhost (10061)

    mysql数据库出现2003-Can't connect to mysql server on localhost (10061)问题 解决办法:查看wampserver服务器是否启动,如果没有启动启 ...

  4. Error connecting to database [Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)]

    参照 http://stackoverflow.com/questions/4448467/cant-connect-to-local-mysql-server-through-socket-var- ...

  5. HTTP Method详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)

    前言 HTTP Method的历史: HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这三个方法 HTTP 1.1 这个版本是当前版本,包含GET HE ...

  6. IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API

    IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习之保护API. 使用IdentityServer4 来实现使用客户端凭据保护ASP.N ...

  7. Connect to the DSP on C6A8168/DM8168/DM8148 using CCS

    转自ti-wiki  这份wiki,我曾经就收藏过,但是没有加以重视,以至于绕了一大圈的ccs开发环境的配置,现在正式收藏于自己的博客中...总结良多啊 Connecting to DSP on C6 ...

  8. Action.c(58): Error -27796: Failed to connect to server "hostname"

    分析: 因为负载生成器的性能太好发数据特别快,服务器响应也特别快,从而导致负载生成器的端口在没有timeout之前就全部占满了. 解决方案一:   在负载生成器的注册表HKEY_LOCAL_MACHI ...

  9. VNC connect:Connection refused(10061)

    在Windows机器上使用VNC Viewer访问Linux服务器,有时候会遇到"connect:Connection refused(10061)"这个错误,导致这个错误出现的原 ...

  10. Oracle Connect by与递归with

    层次查询 select * from emp; select empno, ename, job, mgr, sal, deptno,level lv, sys_connect_by_path(ena ...

随机推荐

  1. spring boot 源码分析

    说明:spring boot版本 2.0.6.RELEASE 思绪 首先,大家认识spring boot是从@SpringBootApplication注解和org.springframework.b ...

  2. mysql 1055

    在 /etc/my.cnf 文件里加上如下: sql_mode=NO_ENGINE_SUBSTITUTION

  3. kenlm的使用

    1.训练模型 install_path/bin/lmplz -o -S % -T /temp <text >text.arpa -o  表示n_gram 中的n(必选) -S  内存使用( ...

  4. 图解HTTP第九章

    基于 HTTP 的功能追加协议 1>HTTP 的瓶颈有哪些: 2>消除 HTTP 瓶颈的 SPDY,缩短 Web 页面的加载时间 [1]SPDY 的设计与功能 [2]SPDY 消除 Web ...

  5. 动态库的链接和链接选项-L,-rpath-link,-rpath

    https://my.oschina.net/shelllife/blog/115958 链接动态库 如何程序在连接时使用了共享库,就必须在运行的时候能够找到共享库的位置.linux的可执行程序在执行 ...

  6. linux五种I/O模型

    1.基本概念 1.1同步和异步 同步和异步关注的是消息通信机制 1.1.1同步 所谓同步,就是在发出一个调用时,在没有得到结果之前,调用就不返回,一直在等,但是一旦调用返回,就能得到返回值. 1.1. ...

  7. Object constructor

    1. Object is an instance of Function.2. Object does not have a property called constructor so when w ...

  8. webpack踩坑--webpack 2.x升级至4.x

    一.安装webpack-cli,webpack@4.26.1 1.npm install webpack-cli -D 2.npm install webpack@4.26.1 -D 二.踩坑 执行n ...

  9. 学以致用三十四-----python2.0加载图片

    想用做一个静态图片为背景的页面.结果遇到了一些阻碍.其主要原因还是路径没有找对.网上也参考了不少方法,也许是因为版本不同,处理的方法也不同,因此按照网上的处理方式,也没有得到解决. 为此困惑了一天.结 ...

  10. python PyInstaller 库

    https://www.cnblogs.com/gopythoner/p/6337543.html https://www.cnblogs.com/duan-qs/p/6548875.html htt ...