PC6下载站

分类分类

oracle递归查询

关注+2010-01-20作者:

通过子节点向根节点追朔.

  Sql代码

  select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid

  通过根节点遍历子节点.

  Sql代码

  select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid

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

  Sql代码

  select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid

  备注:

  start with ...connect by 的用法, start with 后面所跟的就是就是递归的种子。

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

  connect by prior 后面所放的字段是有关系的,它指明了查询的方向。

  练习: 通过子节点获得顶节点

  Sql代码

  select FIRST_VALUE(deptid) OVER (ORDER BY LEVEL DESC ROWS UNBOUNDED PRECEDING) AS firstdeptid from persons.dept start with deptid=76 connect by prior paredeptid=deptid

表结构
DEPTID

PAREDEPTID

NAME
NUMBER NUMBER CHAR (40 Byte)
部门id 父部门id(所属部门id) 部门名称

 

展开全部

相关文章

更多+相同厂商

热门推荐

  • 最新排行
  • 最热排行
  • 评分最高
排行榜

    点击查看更多

      点击查看更多

        点击查看更多

        说两句网友评论

          我要评论...
          取消