Oracle connect by prior理解

本文通过四个SQL示例,详细解析了Connect By语句在Oracle中的应用方式,包括如何使用Start With结合过滤条件进行层级查询。

一、语法

SQL1: select * from goods_type connect by prior id = parent_id start with id = 213;
SQL2: select * from goods_type connect by prior id = parent_id and name like '%皮革%' start with id = 213;
SQL3: select * from goods_type where name like '%皮革%' connect  by prior id = parent_id  start with id = 213;

SQL4: select * from goods_type connect by prior id = parent_id  start with id = 1 and name like '%口%'

结果:

sql1:   查出  id为213  及  其子记录(所有)

sql2:   先过滤 再  查询(每层的查询都去过滤,只保留符合条件的子分支)

sql3:  先查询 再 过滤(全部查完之后,对整个结果集过滤)

sql4:  对起始条件的一个过滤,即从   id为1 name又包含 口的 记录开始查起

二、理解

以上面的例子为例

(1)先查出select id from organ where id = 1(即start with 的条件),查出来的结果作为第一条数据。

(2) 再看connect by ,现在是prior id = parentid,即上一条的id 是下一条的parentid,

那么这个查出来的就是 id=1的所有孩子以及它自身

如果要查 id= 1的父辈, 那么就应该是 第一条的parentid 是下一条的id,即 prior parentid = id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值