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

基本语法:

<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;
时间: 2024-08-03 09:28:18

Oracle start with...connect by prior 实现递归查询的相关文章

Oracle start with connect by prior 用法

Oracle start with connect by prior 用法  语法: select * from 表名 where 条件1 start with 条件2 connect by prior 当前表字段=级联表字段 start with与connect by prior语句完成递归记录,形成一棵树形结构,通常可以在具有层次结构的表中使用. start with表示开始的记录 connect by prior 指定与当前记录关联时的字段关系代码: --创建部门表,这是一个具有层次结构的

在oracle中通过connect by prior来实现递归查询!

注明:该文章为引用别人的文章,链接为:http://blog.csdn.net/apicescn/article/details/1510922 ,本人记录下来只是为了方便查看 原文: connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start by cond1 connect by cond2 where cond3; 简单说来是将一个树状结构存储在一张表里,比如一个表中存在两个字段: id,parentid那么通过表示每一条记录

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

start with connect by prior 主要是用于B树结构类型的数据递归查询,给出B树结构类型中的任意一个节点,遍历其最终父节点或者子节点. -- create table create table prior_test ( parentid number(10), subid number(10) ); -- 字段类型最好用 number,而不是 varchar2,因为测试 SQL 需要比较 id -- insert insert into prior_test values

在Oracle 中使用CONNECT BY PRIOR START WITH 语句详解

语法:connect by 是结构化查询中用到的,其基本语法如下: start with,connect by主要目的:从表中取出树状数据.可以假想成表中存成的各条数据是分别是树中的一个结点. select … from tablename start with 条件1 connect by prior 条件2 where 条件3: 其中: 条件1 是根结点限定语句,当然也可以放宽限定条件取多个结点,就形成多根树. 条件2 是连接条件. 条件3 是过滤条件,用于所返回的所有记录进行过滤. 例如

Oracle高级函数篇之递归查询start with connect by prior简单用法

路飞:" 把原来CSDN的博客转移到博客园咯!" 前段时间,自己负责的任务中刚好涉及到了组织关系的业务需求,自己用了oracle递归查询.下面简单来举个例子.在工作中我们经常会遇到有一定组织关系层次的关系.比如某个省下有多少市,每个市下又有多个区.再或者公司组织部门相互的隶属关系.这时我们就可能会用到 start with connect by prior 递归查询了 用法举例 1. 基本sql语法: select  ...   from  + 表   start with      

oracle中 connect by prior 递归查询

Oracle中start with...connect by prior子句用法 connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start with 条件1 connect by 条件2 where 条件3; 例: select * from table start with org_id = 'HBHqfWGWPy' connect by prior org_id = parent_id; 简单说来是将一个树状结构存储在一张表里

oracle 中 Start with...connect by 的用法(递归查询)

阿里电面问到了相关的知识,在网上找到这方面的文章. 这几个关键字是查询递归数据的,形成一个树状结构.目前只有oracle支持,其他数据都要结合存储过程实现 语法: select * from some_table [where 条件1] connect by [条件2] start with [条件3]; 其中 connect by 与 start with 语句摆放的先后顺序不影响查询的结果,[where 条件1]可以不需要.  [where 条件1].[条件2].[条件3]各自作用的范围都不

Oracle高级函数1------ Oracle 树操作(select…start with…connect by…prior)

oracle树查询的最重要的就是select…start with…connect by…prior语法了.依托于该语法,我们可以将一个表形结构的以树的顺序列出来.在下面列述了oracle中树型查询的常用查询方式以及经常使用的与树查询相关的oracle特性函数等,在这里只涉及到一张表中的树查询方式而不涉及多表中的关联等. 1.准备测试表和测试数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

oracle树操作(select .. start with .. connect by .. prior)

oracle中的递归查询可以使用:select .. start with .. connect by .. prior 下面将会讲述oracle中树形查询的常用方式,只涉及到一张表. 一. 建表语句: -- 菜单目录结构表 create table tb_menu( id number(10) not null, -- 主键id title varchar2(50), -- 标题 parent number(10) -- parent id ) -- 父菜单 insert into tb_me