oracle中 sql语句:start with .. connect by prior.. 用法
2017-03-17 11:12
302 查看
转载自:http://blog.csdn.net/jiangyu1013/article/details/52936620
我们经常会将一个比较复杂的目录树存储到一个表中。或者将一些部门存储到一个表中,而这些部门互相有隶属关系。这个时候你就会用到connect by prior start with。Oracle 提供了start
with connect by 语法结构可以实现递归查询。
connect by 是结构化查询中用到的,其基本语法是:
select ... from tablename start with 条件1
connect by prior 条件2
where 条件3;
例:
select * from table
start with org_id = 'HBHqfWGWPy'
connect by prior org_id = parent_id;
简单说来是将一个树状结构存储在一张表里,比如一个表中存在两个字段:
org_id,parent_id那么通过表示每一条记录的parent是谁,就可以形成一个树状结构。
用上述语法的查询可以取得这棵树的所有记录。
其中:
条件1 是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。
条件2 是连接条件,其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR org_id = parent_id就是说上一条记录的org_id 是本条记录的parent_id,即本记录的父亲是上一条记录。
条件3 是过滤条件,用于对返回的所有记录进行过滤。
例如:
没有加中start with ... connect by prior ...的查询结果:
select t.dim_id, t.pid, level
from pmcode.pmcode_fj_tree_rl t
where t.dim_id in (select b.dim_id
from pmcode.PMCODE_KPI_DIM_OD b
where b.kpi_id = 'KC0011')
结果:
DIM_ID PID LEVEL
---------------------
1024 5003 0
1070 0 0
5003 1070 0
5006 0 0
------------------------------------------------------------------------------------
增加start with ... connect by prior ...以后的结果:
select t.dim_id, t.pid, level
from pmcode.pmcode_fj_tree_rl t
where t.dim_id in (select b.dim_id
from pmcode.PMCODE_KPI_DIM_OD b
where b.kpi_id = 'KC0011')
start with t.dim_id = '1070' ----表示从dim_id = '1070'开始(也就是说1070为根节点)
connect by prior t.dim_id = t.pid; ----表示上条记录的dim_id等于本条记录的pid
结果:
DIM_ID PID LEVEL
---------------------
1070 0 1
5003 1070 2
1024 5003 3
又如:
1. 一个简单举例:
SQL> select * from
test;
BILL_MONTH DAY_NUMBER
MSISDN
-------------------- ---------- --------------------
200803 1
13800
200803 3
13800
200803 2
13800
200803 2
13801
200803 4
13804
200803 5
13804
200803 7
13804
200803 8
13804
200803 6
13802
200803 6
13801
200803
我们经常会将一个比较复杂的目录树存储到一个表中。或者将一些部门存储到一个表中,而这些部门互相有隶属关系。这个时候你就会用到connect by prior start with。Oracle 提供了start
with connect by 语法结构可以实现递归查询。
connect by 是结构化查询中用到的,其基本语法是:
select ... from tablename start with 条件1
connect by prior 条件2
where 条件3;
例:
select * from table
start with org_id = 'HBHqfWGWPy'
connect by prior org_id = parent_id;
简单说来是将一个树状结构存储在一张表里,比如一个表中存在两个字段:
org_id,parent_id那么通过表示每一条记录的parent是谁,就可以形成一个树状结构。
用上述语法的查询可以取得这棵树的所有记录。
其中:
条件1 是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。
条件2 是连接条件,其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR org_id = parent_id就是说上一条记录的org_id 是本条记录的parent_id,即本记录的父亲是上一条记录。
条件3 是过滤条件,用于对返回的所有记录进行过滤。
例如:
没有加中start with ... connect by prior ...的查询结果:
select t.dim_id, t.pid, level
from pmcode.pmcode_fj_tree_rl t
where t.dim_id in (select b.dim_id
from pmcode.PMCODE_KPI_DIM_OD b
where b.kpi_id = 'KC0011')
结果:
DIM_ID PID LEVEL
---------------------
1024 5003 0
1070 0 0
5003 1070 0
5006 0 0
------------------------------------------------------------------------------------
增加start with ... connect by prior ...以后的结果:
select t.dim_id, t.pid, level
from pmcode.pmcode_fj_tree_rl t
where t.dim_id in (select b.dim_id
from pmcode.PMCODE_KPI_DIM_OD b
where b.kpi_id = 'KC0011')
start with t.dim_id = '1070' ----表示从dim_id = '1070'开始(也就是说1070为根节点)
connect by prior t.dim_id = t.pid; ----表示上条记录的dim_id等于本条记录的pid
结果:
DIM_ID PID LEVEL
---------------------
1070 0 1
5003 1070 2
1024 5003 3
又如:
1. 一个简单举例:
SQL> select * from
test;
BILL_MONTH DAY_NUMBER
MSISDN
-------------------- ---------- --------------------
200803 1
13800
200803 3
13800
200803 2
13800
200803 2
13801
200803 4
13804
200803 5
13804
200803 7
13804
200803 8
13804
200803 6
13802
200803 6
13801
200803
相关文章推荐
- oracle中 sql语句:start with .. connect by prior.. 用法
- MySql的sql语句中添加存储过程或者存储函数来实现Oracle中的start with ……connect by prior……递归(树形结构数据)查询
- Oracle_start_with_connect_by_prior_用法
- ORACLE start with… connect by prior 子句用法
- Oracle中START WITH...CONNECT BY PRIOR用法
- Oracle树结构查询 start with ... connect by用法简介&sql有向图问题期待新解决方案
- oracle中START WITH ... CONNECT BY PRIOR的用法
- Oracle start with connect by prior 用法
- Oracle start with connect by prior 递归查询用法
- Oracle start with ... connect by prior 用法
- oracle--SQL之树(start with... connect by prior...)
- 在Oracle 中使用CONNECT BY PRIOR START WITH 语句详解
- Oracle start with ... connect by prior 用法
- ORACLE 层次结构查询语句 start with connect by prior
- oracle 中的select ...connect by prior ...start with 及(+)的用法
- Oracle START WITH...CONNECT BY PRIOR的用法
- Oracle start with connect by prior 用法(递归查询树形结构)
- oracle start with connect by prior 递归查询用法
- 如何在sqlserver实现oracle中START WITH id = '' CONNECT BY PRIOR id=p_id语句
- oracle 中的select ...connect by prior ...start with 及(+)的用法