Oracle start with...connect by prior 实现递归查询
来源:程序员人生 发布时间:2014-12-14 08:15:51 阅读次数:3690次
基本语法:
<span style="font-size:18px;"><em>select ... from <TableName>
where <Conditional⑴>
start with <Conditional⑵>
connect by <Conditional⑶>;
<Conditional⑴>:过滤条件,用于对返回的所有记录进行过滤。
<Conditional⑵>:查询结果重起始根结点的限定条件。
<Conditional⑶>:连接条件</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;
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠