start with connect by prior 递归查询用法
来源:互联网 发布:ubuntu 16.04 新特性 编辑:程序博客网 时间:2024/06/16 07:28
start with connect by prior 递归查询用法
这个子句主要是用于B树结构类型的数据递归查询,给出B树结构类型中的任意一个结点,遍历其最终父结点或者子结点。
先看原始数据:
1 create table a_test 2 ( parentid varchar2(10), 3 subid varchar2(10)); 4 5 insert into a_test values ( '1', '2' ); 6 insert into a_test values ( '1', '3' ); 7 insert into a_test values ( '2', '4' ); 8 insert into a_test values ( '2', '5' ); 9 insert into a_test values ( '3', '6' );10 insert into a_test values ( '3', '7' );11 insert into a_test values ( '5', '8' );12 insert into a_test values ( '5', '9' );13 insert into a_test values ( '7', '10' );14 insert into a_test values ( '7', '11' );15 insert into a_test values ( '10', '12' );16 insert into a_test values ( '10', '13' );17 18 commit;19 20 select * from a_test;
对应B树结构为:
接下来看一个示例:
要求给出其中一个结点值,求其最终父结点。以7为例,看一下代码
start with 子句:遍历起始条件,有个小技巧,如果要查父结点,这里可以用子结点的列,反之亦然。
connect by 子句:连接条件。关键词prior,prior跟父节点列parentid放在一起,就是往父结点方向遍历;prior跟子结点列subid放在一起,则往叶子结点方向遍历,
parentid、subid两列谁放在“=”前都无所谓,关键是prior跟谁在一起。
order by 子句:排序,不用多说。
--------------------------------------------------
下面看看往叶子结点遍历的例子:
这里start with 子句用了parentid列,具体区别后面举例说明。
connect by 子句中,prior跟subid在同一边,就是往叶子结点方向遍历去了。因为7有两个子结点,所以第一级中有两个结果(10和11),10有两个子结点(12,13),11无,所以第二级也有两个结果(12,13)。即12,13就是叶子结点。
下面看下start with子句中选择不同的列的区别:
以查询叶子结点(往下遍历)为例
结果很明显,原意是要以7为父结点,遍历其子结点,左图取的是父结点列的值,结果符合原意;右图取的是子结点列的值,结果多余的显示了7 的父结点3.
---------------------------------------
关于where条件的语句,以后验证后再记录。先留个疑问
- start with connect by prior 递归查询用法
- start with connect by prior 递归查询用法
- start with connect by prior 递归查询用法
- start with connect by prior 递归查询用法
- Oracle start with connect by prior 递归查询用法
- oracle start with connect by prior 递归查询用法
- 递归查询START WITH CONNECT BY PRIOR
- 递归查询 START WITH CONNECT BY PRIOR
- 用START WITH...CONNECT BY PRIOR子句实现递归查询
- oracle关键字 start with、、、 connect by prior 子句递归查询
- 让hibernate支持递归、start with connect by prior 查询
- oracle 递归查询 start with connect by prior
- 让hibernate支持递归、start with connect by prior 查询
- 让hibernate支持递归、start with connect by prior 查询
- Start with connect by prior 递归 树查询
- Oracle start with......connect by prior......子句实现递归查询
- Oracle start with.connect by prior子句实现递归查询
- Oracle start with.connect by prior子句实现递归查询
- 笔记: Field 与 Method
- oracle 练习 之关联查询 练习
- 【web开发】:JSP连接Mysql数据库(非常详细,代码一看就懂)
- LeetCode 204 Count Primes
- Java sdut acm 3338 计算各种图形的周长(接口与多态)
- start with connect by prior 递归查询用法
- 鸟哥的服务器《六》SELinux管理原则
- c 程序设计语言 第二版 练习题 4-10
- JSP基本部署
- checkbox/radio onchange不能兼容ie8 如何处理?
- 唯一索引与主键索引的比较
- 多个无线AP间无线组网实例
- 二叉树的性质综合
- L