我们经常会将一个比较复杂的目录树存储到一个表中。或者将一些部门存储到一个表中,而这些部门互相有隶属关系。这个时候你就会用到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

oracle中 start with .. connect by prior.. 用法简介的更多相关文章

  1. Oracle中start with...connect by (prior)子句的用法

    connect by 是结构化查询中用到的,基本语法是:select … from tablenamestart with 条件1connect by 条件2where 条件3; 例:select * ...

  2. oracle 中的select ...connect by prior ...start with 及(+)的用法

    1.select ...connect by prior ...start with的用法: select ... from <tablename> where <condition ...

  3. oracle的START WITH CONNECT BY PRIOR用法

    转自:https://www.cnblogs.com/linjiqin/archive/2013/06/24/3152674.html Oracle 树操作(select…start with…con ...

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

    阿里电面问到了相关的知识,在网上找到这方面的文章. 这几个关键字是查询递归数据的,形成一个树状结构.目前只有oracle支持,其他数据都要结合存储过程实现 语法: select * from some ...

  5. Oracle start with connect by prior 用法

    Oracle start with connect by prior 用法    语法: select * from 表名 where 条件1 start with 条件2 connect by pr ...

  6. ORACLE 中的 ROW_NUMBER() OVER() 分析函数的用法

    ORACLE 中的 ROW_NUMBER() OVER() 分析函数的用法 ROW_NUMBER() OVER(partition by col1 order by col2) 表示根据col1分组, ...

  7. oracle中的exists 和not exists 用法 in与exists语句的效率问题

    博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源(  in与exi ...

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

    一.基本语法 connect by递归查询基本语法是: select 1 from 表格 start with ... connect by prior id = pId start with:表示以 ...

  9. oracle的start with connect by prior

    oracle的start with connect by prior是根据条件递归查询"树",分为四种使用情况: 第一种:start with 子节点ID='...' connec ...

随机推荐

  1. PHP读取远程文件的三种方法

    file_get_contents <?php$url = http://www.xxx.com/;$contents = file_get_contents($url);//如果出现中文乱码使 ...

  2. mysql增删改查基本语句

    mysql的增删改查属于基本操作,又被简称CRUD,其中删用的较少,毕竟这个功能给用户是是非常危险的,就是客户删除的数据也没有真正的删除,其中查询是十分常用的. 1 mysql数据库增加:create ...

  3. 【IOS6.0 自学瞎折腾】(五)应用程序的启动过程和Application生命周期

    一 :main函数入口 看下项目资源结构,其实程序的入口也是在main.m里面. #import <UIKit/UIKit.h> #import "BvinAppDelegate ...

  4. C++中的inline关键字

    from here 1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程 ...

  5. LeetCode——Contains Duplicate II

    Description: Given an array of integers and an integer k, find out whether there there are two disti ...

  6. luogu P1379 八数码难题(A*算法入门详细讲解)

     代码实现细节 #include<cstdio> #include<cstring> #include<iostream> using namespace std; ...

  7. WEB安全第七篇--终结篇考验逻辑思维:逻辑漏洞大汇总(越权、会话逻辑、业务逻辑、暴力破解)

    零.前言 最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件.底层安全.漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的pay ...

  8. python epoll实现异步socket

    一.同步和异步: 在程序执行中,同步运行意味着等待调用的函数.线程.子进程等的返回结果后继续处理:异步指不等待当下的返回结果,直接运行主进程下面的程序,等到有返回结果时,通知主进程处理.有点高效. 二 ...

  9. URLSearchParams 接口定义处理 URL 参数串

    基本使用方法如下 /* * URLSearchParams属性 * @语法:new URLSearchParams(parameter); */ (function(){ var str = &quo ...

  10. 【BZOJ4452】[Cerc2015]Export Estimate 并查集

    [BZOJ4452][Cerc2015]Export Estimate Description 给你一个n个点m条边的无向图,每条边有权值,我们可以选择一个整数lim来生成一个新的图,过程如下:  1 ...