码迷,mamicode.com
首页 > 数据库 > 详细

Oracle start with...connect by prior 实现递归查询

时间:2014-12-05 19:23:07      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   sp   on   数据   2014   

基本语法:

<span style="font-size:18px;"><em>select ... from <TableName>
where <Conditional-1>
start with <Conditional-2>
connect by <Conditional-3>;

<Conditional-1>:过滤条件,用于对返回的所有记录进行过滤。
<Conditional-2>:查询结果重起始根结点的限定条件。
<Conditional-3>:连接条件</em></span>


数据表结构如下:

create table table_test(
priorId number,
id number,
name varchar(5),
description varchar(10)
);


测试数据:

insert into table_test(priorId,id,name,description) values(0,1,'f','f父类');
insert into table_test(priorId,id,name,description) values(1,2,'f1','f子类1');
insert into table_test(priorId,id,name,description) values(1,3,'f2','f子类2');
insert into table_test(priorId,id,name,description) values(0,4,'F','F父类');
insert into table_test(priorId,id,name,description) values(4,5,'F1','F子类1');
insert into table_test(priorId,id,name,description) values(4,6,'F2','F子类2');

 

获取完整树:

select * from table_test start with priorId = 0 connect by prior id = priorId;


获取特定子树:

select * from table_test start with id = 1 connect by prior id = priorId;
select * from table_test start with id = 4 connect by prior id = priorId;


如果connect by prior中的prior被省略,则查询将不进行深层递归:

select * from table_test start with priorId = 0 connect by id = priorId;
select * from table_test start with id = 1 connect by id = priorId;


 


 

 

 

 

 

 

Oracle start with...connect by prior 实现递归查询

标签:des   style   blog   io   ar   sp   on   数据   2014   

原文地址:http://blog.csdn.net/helloboat/article/details/41750527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!