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>:连接条件
数据组织结构如下图:
|---a------a1
| |------a2
|
|---b------b1
|------b2
数据库表结构如下:
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');
SQL> select * from t2;
ROOT_ID ID NAME DESCRIPTIO
---------- ---------- ----- ----------
0 1 a aaa
1 2 a1 aaa1
1 3 a2 aaa2
0 4 b bbb
4 5 b1 bbb1
4 6 b2 bbb2
获取完整树:
select * from t2 start with root_id = 0 connect by prior id = root_id;
|---a------a1
| |------a2
|
|---b------b1
|------b2
ROOT_ID ID NAME DESCRIPTIO
---------- ---------- ----- ----------
0 1 a aaa
1 2 a1 aaa1
1 3 a2 aaa2
0 4 b bbb
4 5 b1 bbb1
4 6 b2 bbb2
获取特定子树:
select * from t2 start with id = 1 connect by prior id = root_id;
|---a------a1
| |------a2
ROOT_ID ID NAME DESCRIPTIO
---------- ---------- ----- ----------
0 1 a aaa
1 2 a1 aaa1
1 3 a2 aaa2
select * from t2 start with id = 4 connect by prior id = root_id;
|---b------b1
|------b2
ROOT_ID ID NAME DESCRIPTIO
---------- ---------- ----- ----------
0 4 b bbb
4 5 b1 bbb1
4 6 b2 bbb2
如果connect by prior中的prior被省略,则查询将不进行深层递归。
如:
select * from t2 start with root_id = 0 connect by id = root_id;
|---a
|
|
|---b
ROOT_ID ID NAME DESCRIPTIO
---------- ---------- ----- ----------
0 1 a aaa
0 4 b bbb
如:
select * from t2 start with id = 1 connect by id = root_id;
|---a
ROOT_ID ID NAME DESCRIPTIO
---------- ---------- ----- ----------
0 1 a aaa
原文地址:http://hi.baidu.com/learnfordba/blog/item/28fe4608fcb2112d6a60fbdb.html
分享到:
相关推荐
本文章详细介绍了Oracle中connect by...start with...的用法。
Connect By 可以列出上下级关系 构造序列 求排列组合 逆转求出下上级的关系路径
oracle中 connect by prior 递归算法 Oracle中start with...connect by prior子句用法 connect by 是结构化查询中用到的
oracle中的数查询,介绍的详细,有例子。
一、基本语法 connect by递归查询基本语法是: select 1 from 表格 start with … connect by prior id = pId start with:表示以什么为根节点,不加限制可以写1=1,要以id为123的节点为根节点,就写为start ...
。。。
。。。
行列转换,层级关系,oracle sys_connect_by_path的用法
NULL 博文链接:https://yunqiang-zhang-hotmail-com.iteye.com/blog/1312354
Oracle_start_with_connect_by_prior_用法[文].pdf
Oracle 数据库树形结构用法总结,例如SYS_CONNECT_BY_PATH 、START WITH . . . CONNECT BY . . .等具体语法介绍
很多场合我们都会用到...sys_connect_by_path(字段名, 2个字段之间的连接符号),这里的连接符号不要使用逗号,oracle会报错,如果一定要用,可以使用replace替换一下,方法如下 REPLACE(字段名,原字符,’,’)。这个
今天无意间,看connect by的使用,看到了sys_connect_by_path的用法,算是给我一个另类的惊喜了,sys_connect_by_path(columnname, seperator) 也可以拼出串来,不过这个函数本身不是用来给我们做这个结果集连接用...
7.1 基本的GROUP BY用法 171 7.2 HAVING子句 174 7.3 GROUP BY的“新”功能 175 7.4 GROUP BY的CUBE扩展 175 7.5 CUBE的实际应用 179 7.6 通过GROUPING()函数排除空值 185 7.7 用GROUPING()来扩展报告 186 ...
oracle 常用函数,connect by用法,分页方法
Oracle 递归查询, start with connect by prior 用法 find_in_set 函数 concat,concat_ws,group_concat 函数 MySQL 自定义函数 手动实现 MySQL 递归查询 Oracle 递归查询 在 Oracle 中是通过 start ...
(SID_NAME = cams) ) (SID_DESC = (GLOBAL_DBNAME = oid) (ORACLE_HOME = /u01/app/oracle/product/8.1.7) (SID_NAME = oid) ) ) <br>& 说明 1 listener.ora 文件中定义一个监听器...
下面按类别列出一些ORACLE用户常用数据字典的查询使用方法。 1、用户 查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user_...