Oracle Connect By Prior用法
2016-01-14 12:40
645 查看
racle中的select语句可以用START WITH...CONNECT BY PRIOR子句实现递归查询,connect by 是结构化查询中用到的,其基本语法是:
select ... from <TableName>
where <Conditional-1>
start with <Conditional-2>
connect by <Conditional-3>
;
<Conditional-1>:过滤条件,用于对返回的所有记录进行过滤。
<Conditional-2>:查询结果重起始根结点的限定条件。
<Conditional-3>:连接条件
数据组织结构如下图:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_1.jpg)
数据库表结构如下:
create table t2(
root_id number,
id number,
name varchar(5),
description varchar(10)
);
insert into t2(root_id,id,name,description) values(0,1,'a','aaa');
insert into t2(root_id,id,name,description) values(1,2,'a1','aaa1');
insert into t2(root_id,id,name,description) values(1,3,'a2','aaa2');
insert into t2(root_id,id,name,description) values(0,4,'b','bbb');
insert into t2(root_id,id,name,description) values(4,5,'b1','bbb1');
insert into t2(root_id,id,name,description) values(4,6,'b2','bbb2');
获取完整树:
select * from t2 start with root_id = 0 connect by prior id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_2.jpg)
获取特定子树:
select * from t2 start with id = 1 connect by prior id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_3.jpg)
select * from t2 start with id = 4 connect by prior id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_4.jpg)
如果connect by prior中的prior被省略,则查询将不进行深层递归。
如:
select * from t2 start with root_id = 0 connect by id = root_id;
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_5.jpg)
select * from t2 start with id = 1 connect by id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_6.jpg)
关于PRIOR
运算符PRIOR被放置于等号前后的位置,决定着查询时的检索顺序。
PRIOR被置于CONNECT BY子句中等号的前面时,则强制从根节点到叶节点的顺序检索,即由父节点向子节点方向通过树结构,我们称之为自顶向下的方式。如:
CONNECT BY PRIOR EMPNO=MGR
PIROR运算符被置于CONNECT BY 子句中等号的后面时,则强制从叶节点到根节点的顺序检索,即由子节点向父节点方向通过树结构,我们称之为自底向上的方式。例如:
CONNECT BY EMPNO=PRIOR MGR 等价于
connect by prior mgr=empno
其实跟在等号哪边没关系,主要是明白empno 和mgr 是子--父 关系,只要记住prior 和子 字段在一起,表示寻找他的子孙,和父 字段在一起,表示开始寻找她的爸爸!
select ... from <TableName>
where <Conditional-1>
start with <Conditional-2>
connect by <Conditional-3>
;
<Conditional-1>:过滤条件,用于对返回的所有记录进行过滤。
<Conditional-2>:查询结果重起始根结点的限定条件。
<Conditional-3>:连接条件
数据组织结构如下图:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_1.jpg)
数据库表结构如下:
create table t2(
root_id number,
id number,
name varchar(5),
description varchar(10)
);
insert into t2(root_id,id,name,description) values(0,1,'a','aaa');
insert into t2(root_id,id,name,description) values(1,2,'a1','aaa1');
insert into t2(root_id,id,name,description) values(1,3,'a2','aaa2');
insert into t2(root_id,id,name,description) values(0,4,'b','bbb');
insert into t2(root_id,id,name,description) values(4,5,'b1','bbb1');
insert into t2(root_id,id,name,description) values(4,6,'b2','bbb2');
获取完整树:
select * from t2 start with root_id = 0 connect by prior id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_2.jpg)
获取特定子树:
select * from t2 start with id = 1 connect by prior id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_3.jpg)
select * from t2 start with id = 4 connect by prior id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_4.jpg)
如果connect by prior中的prior被省略,则查询将不进行深层递归。
如:
select * from t2 start with root_id = 0 connect by id = root_id;
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_5.jpg)
select * from t2 start with id = 1 connect by id = root_id;
如:
![](http://image30.360doc.com/DownloadImg/2011/06/0811/12596501_6.jpg)
关于PRIOR
运算符PRIOR被放置于等号前后的位置,决定着查询时的检索顺序。
PRIOR被置于CONNECT BY子句中等号的前面时,则强制从根节点到叶节点的顺序检索,即由父节点向子节点方向通过树结构,我们称之为自顶向下的方式。如:
CONNECT BY PRIOR EMPNO=MGR
PIROR运算符被置于CONNECT BY 子句中等号的后面时,则强制从叶节点到根节点的顺序检索,即由子节点向父节点方向通过树结构,我们称之为自底向上的方式。例如:
CONNECT BY EMPNO=PRIOR MGR 等价于
connect by prior mgr=empno
其实跟在等号哪边没关系,主要是明白empno 和mgr 是子--父 关系,只要记住prior 和子 字段在一起,表示寻找他的子孙,和父 字段在一起,表示开始寻找她的爸爸!
相关文章推荐
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- Oracle Containers for J2EE远程安全漏洞(CVE-2014-0413)
- Oracle 10g R2不能使用EM的问题
- 表空间操作
- PreparedStatement中in子句的处理
- VMware下RedHat4.8_64位安装Oracle 10g RAC--简略脚本
- oracle sql日期比较
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- OS block size和Oracle block size,查找OS Blocksize的方法
- oracle中创建数据库和表空间的几点总结
- 数据库自动备份脚本
- oracle的nvl函数的使用介绍
- 解决oracle用户连接失败的解决方法
- oracle的一些tips技巧
- Oracle 下的开发日积月累
- Oracle存储过程之数据库中获取数据实例
- Windows下ORACLE 10g完全卸载的方法分析
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ORACLE LATERAL-SQL-INJECTION 个人见解