带您了解Oracle层次查询
Oracle 查询 了解 层次
2023-09-14 08:58:24 时间
http://database.51cto.com/art/201010/231539.htm
Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的基本语法,希望可以让您对Oracle层次查询有更深的认识。
oracle中的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>:连接条件
数据组织结构如下图:
数据库表结构如下:
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;
获取特定子树:
select * from t2 start with id = 1 connect by prior id = root_id;
select * from t2 start with id = 4 connect by prior id = root_id;
如果connect by prior中的prior被省略,则查询将不进行深层递归。
如:
select * from t2 start with root_id = 0 connect by id = root_id;
select * from t2 start with id = 1 connect by id = root_id;
如:
![](http://images.51cto.com/files/uploadimg/20101027/1626470.jpg)
![](http://images.51cto.com/files/uploadimg/20101027/1626471.jpg)
![](http://images.51cto.com/files/uploadimg/20101027/1626472.jpg)
![](http://images.51cto.com/files/uploadimg/20101027/1626473.jpg)
![](http://images.51cto.com/files/uploadimg/20101027/1626474.jpg)
![](http://images.51cto.com/files/uploadimg/20101027/1626475.jpg)
相关文章
- oracle查询用户密码命令,Oracle数据库用户密码问题[通俗易懂]
- 优化查询Oracle表的优化之道(oracle表查询)
- 分析Oracle数据库优化查询语句:深入理解执行计划(oracle看执行计划)
- 深入了解Oracle触发器类型(oracle触发器类型)
- Oracle触发器:创建不同类型的数据库触发器(oracle触发器类型)
- Oracle分页:优化查询效率(oracle分页作用)
- 查看Oracle数据库的SID信息(查询oracle的sid)
- 简化Oracle开发运维:Oracle工具类完美解决方案(oracle工具类)
- 查询Oracle数据库的归档模式(查询oracle归档模式)
- Oracle查询表索引:最佳实践(oracle查表索引)
- 优化Oracle数据库SQL优化实践指南(oracle执行sql)
- Efficient Oracle Querying with Looping: Improve Data Retrieval with Loops!(oracle循环查询)
- 使用Oracle内连接语法实现数据库表联合查询(oracle内连接语法)
- C语言调用Oracle实现数据插入(c 调用oracle插入)
- 65535条Oracle记录从0到9999的秘密(65535 oracle)
- 51自学网学习Oracle,一路绽放智慧之花(51自学网oracle)
- Oracle中表关联查询的绝妙融合技巧(oracle中表关联查询)
- Oracle中的自省查询之旅(oracle中自查询)
- Oracle事务处理串行化路径(oracle事务串行)
- Oracle中的快速部分查询(oracle中查询一部分)
- Oracle中如何增加表的实践操作(oracle中增加表操作)
- Oracle使用多种索引解决数据库查询优化(oracle 不唯一索引)
- 基于Oracle的IP地址查询实现(oracle ip查询)