start with connect by prior 递归查询用法
来源:互联网 发布:js面向对象和原型 编辑:程序博客网 时间:2024/06/06 04:13
点击打开链接
先来个总结:
查父节点:start with 后跟子节点列,prior 跟父节点列
查子节点:start with 后跟父节点列,prior 跟子节点列
例一:
select agent_no from agent_info start with agent_no=? connect by prior parent_id=id order by id ;
agent_no=5446 查询编号为5446代理商的所有上级代理商的编号
例二:
select * from ux5_mobile_user u where u.parent_code='000000' start with u.mobile_username=? connect by prior u.parent_code=u.user_code order by id;
下面那一列的 u.mobile_username=222符合 start with条件 根据parent_code=111查询到user_code =111 且筛选出parent_code='000000'的一列
====================================正文=========================================
这个子句主要是用于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子句实现递归查询
- iOS Duplicate interface definition for class 'AppDelegate' ; Property has a previous declaration
- [hash] -- 分布均匀的hash函数
- string,stringbuffer, stringbuffbuilder
- Android中PopupWindow设置背景的方案处理
- Predix 分析应用开发 1
- start with connect by prior 递归查询用法
- 简单记录下Android设计模式 ---个人笔记
- 打开新窗口的window.open使用方法以及最大化等问题解决方法汇总
- eclipse maven异常“Project configuration is not up-to-date with pom.xml
- nginx 获取配置的宏执行过程1
- Java安全架构____KeyGenerator(对称)秘钥生成工具详解
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- 存储过程循环+调用存储过程
- 产品相关的概念