oracle层级查询(Hierarchical Queries)

本文介绍Oracle数据库中进行层级查询的方法及常用伪列的功能,通过示例展示了如何使用CONNECT BY和START WITH子句来构建层级结构,并展示如何获取层级、路径等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

oracle层级查询(Hierarchical Queries)

{ CONNECT BY [ NOCYCLE ] condition [ START WITH condition ]
| START WITH condition CONNECT BY [ NOCYCLE ] condition
}

oracle官网详解

  • 伪列(Pseudocolumn)

oracle层级查询常用的伪列有:

  • level:层级
  • connect_by_isleaf:是否是叶子结点
  • connect_by_iscycle:是否是导致出现死循环的那个树枝
  • SYS_CONNECT_BY_PATH( , ):路径
  • CONNECT_BY_ROOT:根结点

    This graphic shows the order in which Oracle returns the rows for a query of a hierarchical tree. Beginning with the root row, Oracle returns one child row of the root, and then one child of that child row, and so forth until a leaf row (one without children) is encountered. At this point, Oracle goes up in the hierarchy until the next row with a child row is found, and then Oracle returns that child, and so forth until the next leaf row is returned. This process continues, with Oracle returning to the root row for the next child only when all of the "descendants" of the first child row have been returned.

  • 样例(Example )

SELECT last_name "Employee", CONNECT_BY_ROOT last_name "Manager", LEVEL-1 "Pathlen", LTRIM(SYS_CONNECT_BY_PATH(last_name, '/'),'/') "Path"
   FROM employees
   WHERE LEVEL > 1 and department_id = 110
   CONNECT BY PRIOR employee_id = manager_id
   ORDER BY "Employee", "Manager", "Pathlen", "Path";

Employee        Manager            Pathlen Path
--------------- --------------- ---------- ------------------------------
Gietz           Higgins                  1 Higgins/Gietz
Gietz           King                     3 King/Kochhar/Higgins/Gietz
Gietz           Kochhar                  2 Kochhar/Higgins/Gietz
Higgins         King                     2 King/Kochhar/Higgins
Higgins         Kochhar                  1 Kochhar/Higgins
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值