mysql connect by prior start with_start with connect by prior 递归查询用法

这个子句主要是用于B树结构类型的数据递归查询,给出B树结构类型中的任意一个结点,遍历其最终父结点或者子结点。

先看原始数据:

create table a_test

( parentid varchar2(10),

subid varchar2(10));insert into a_test values ( '1', '2' );insert into a_test values ( '1', '3' );insert into a_test values ( '2', '4' );insert into a_test values ( '2', '5' );insert into a_test values ( '3', '6' );insert into a_test values ( '3', '7' );insert into a_test values ( '5', '8' );insert into a_test values ( '5', '9' );insert into a_test values ( '7', '10' );insert into a_test values ( '7', '11' );insert into a_test values ( '10', '12' );insert into a_test values ( '10', '13' );commit;select * from a_test;

3fa9ae41e1538f01b322c4940a697b6b.png

对应B树结构为:

fab5af3c6a6d56f2c52a2e28349ecbe0.png

接下来看一个示例:

要求给出其中一个结点值,求其最终父结点。以7为例,看一下代码

340c60004d81fc4addc5917295b034d5.png

start with 子句:遍历起始条件,有个小技巧,如果要查父结点,这里可以用子结点的列,反之亦然。

connect by 子句:连接条件。关键词prior,prior跟父节点列parentid放在一起,就是往父结点方向遍历;prior跟子结点列subid放在一起,则往叶子结点方向遍历,

parentid、subid两列谁放在“=”前都无所谓,关键是prior跟谁在一起。

order by 子句:排序,不用多说。

--------------------------------------------------

下面看看往叶子结点遍历的例子:

c5e89f30d05cb17a00fcefdc1847d570.png

这里start with 子句用了parentid列,具体区别后面举例说明。

connect by 子句中,prior跟subid在同一边,就是往叶子结点方向遍历去了。因为7有两个子结点,所以第一级中有两个结果(10和11),10有两个子结点(12,13),11无,所以第二级也有两个结果(12,13)。即12,13就是叶子结点。

下面看下start with子句中选择不同的列的区别:

以查询叶子结点(往下遍历)为例

8ce5d0ae1370a1798deac59a84638666.png

结果很明显,原意是要以7为父结点,遍历其子结点,左图取的是父结点列的值,结果符合原意;右图取的是子结点列的值,结果多余的显示了7 的父结点3.

---------------------------------------

关于where条件的语句,以后验证后再记录。先留个疑问

69627db24c7b4255fdbff3b3be5f9f51.png

用另一篇文章总结的分为四种情况:

第一种:start with 子节点ID='...' connect by prior 子节点ID = 父节点ID

select * from mdm_organization o start with o.org_code='10000008' connect by prior o.org_code=o.org_parent_code

按照条件org_code='10000008',对'10000008'(包括自己)及其子节点进行递归查询,结果如下

e3a0840bf28b68cd22a9f92caee60d14.png

查询结果自己所有的后代节点(包括自己)。

第二种:start with 子节点ID='...' connect by 子节点ID = prior 父节点ID

select * from mdm_organization o start with o.org_code='10000008' connect by o.org_code=prior o.org_parent_code

按照条件org_code='10000008',对'10000008'(包括自己)及其父节点进行递归查询,结果如下

80f44b14631ff0bef8c649201e129610.png

查询结果自己所有的前代节点(包括自己)。

第三种:start with 父节点ID='...' connect by prior 子节点ID = 父节点ID

select * from mdm_organization o start with o.org_parent_code='10000008' connect by prior o.org_code=o.org_parent_code

按照条件org_parent_code='10000008',对'10000008'(不包括自己)子节点进行递归查询,结果如下

b8319859e232b3534a2ad17c4abbd720.png

查询结果自己所有的后代节点(不包括自己)。

第四种:start with 父节点ID='...' connect by 子节点ID = prior 父节点ID

select * from mdm_organization o start with o.org_parent_code='10000008' connect by o.org_code = prior o.org_parent_code

按照条件org_parent_code='10000008',对'10000008'(包括自己)的第一代孩子们及其父节点进行递归查询,结果如下

a89984fae041c1ab7ef674171e0d403b.png

查询结果自己的第一代后节点和所有的前代节点(包括自己)。

如果有where 条件,如下

select * from mdm_organization o where 条件 start with o.org_parent_code='10000008' connect by o.org_code = prior o.org_parent_code

执行顺序为先执行start with connect by prior,然后再按照where条件进行过滤。

参考文章:

https://www.cnblogs.com/benbenduo/p/4588612.html

https://www.cnblogs.com/lirun/p/9116337.html

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值