首页 > 数据库技术 > 详细

Oracle_071_lesson_p21

时间:2018-08-14 17:43:57      阅读:194      评论:0      收藏:0      [点我收藏+]
Hierarchical Retrieval 层次检索

you should be able to:
1、Interpret the concept of a hierarchical query
2、Create a tree-structured report
3、Format hierarchical data
4、Exclude branches from the tree structure
技术分享图片

SELECT [LEVEL], column, expr...
FROM table
[WHERE condition(s)]
[START WITH condition(s)]
[CONNECT BY PRIOR condition(s)] ;

例:
SELECT employee_id, last_name, job_id, manager_id
FROM employees
START WITH employee_id = 101
CONNECT BY PRIOR manager_id = employee_id ;

例:
SELECT last_name||‘ reports to ‘||
PRIOR last_name "Walk Top Down"
FROM employees
START WITH last_name = ‘King‘
CONNECT BY PRIOR employee_id = manager_id ;

例:
select employee_id , last_name , manage_id , level
from emp
[可选] where employee_id <>102 裁去102号人
start with employee=100;
connnect by prior employee_id = manage_id;
[可选]and employee_id <> 102; 裁去102号人以下所有人(tree树结构)

connect by prior 只关心其后紧跟的字段,如果是主键,则从上往下检索,如果是子键,则从下往上检索.

技术分享图片

COLUMN org_chart FORMAT A12;
SELECT LPAD(last_name, LENGTH(lastname)+(LEVEL*2)-2,‘‘)
AS org_chart
FROM employees
START WITH first_name=‘Steven‘ AND last_name=‘King‘
CONNECT BY PRIOR employee_id=manager_id ;

Pruning Branches
技术分享图片

Oracle_071_lesson_p21

原文:http://blog.51cto.com/3938853/2159917

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!