oracle 递归查询

  1. select  *  from t_table start  with id =0 connect   by   prior parentid=id
Sql代码 复制代码
  1. select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid   
 select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid 

 

通过根节点遍历子节点.

  1. select  *  from t_table start  with id =0  connect   by   prior   parentid=id
Sql代码 复制代码
  1. select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid   
select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid 

 

可通过level 关键字查询所在层次.

  1. select t.*, level   from t_table t start  with t.id=0  connect   by   prior t. parentid=t.id
Sql代码 复制代码
  1. select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid   
select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid 

 

start with 后面跟的是递归的种子。

connect by prior 它指明了查询的方向。

connect by 后面的"prior" 如果缺省:则只能查询到符合条件的起始行,并不进行递归查询;

  1. select FIRST_VALUE(id) OVER(ORDER BY LEVEL DESC ROWS UNBOUNDED PRECEDING) AS firstdeptid
      from t_dbinfo_cata
     
    start with
    id = 4
    connect by prior parentid = id
  2. 在start之前可以加入where子句进行条件查询
  3. 在末尾可以加入排序字段
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值