重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4592|回复: 0
打印 上一主题 下一主题

【课堂笔记】【SQL】18.Hierarchical Retrieval

[复制链接]
跳转到指定楼层
楼主
发表于 2012-11-6 10:36:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

解决树状关系查询

示例:

QL> select employee_id,last_name,job_id,manager_id from employees
  2  start with employee_id=101
  3  connect by prior manager_id=employee_id;     

EMPLOYEE_ID LAST_NAME                 JOB_ID     MANAGER_ID
----------- ------------------------- ---------- ----------
        101 Kochhar                   AD_VP             100
        100 King                      AD_PRES

SQL> select last_name || 'report to' || prior last_name "Walk T D"
  2  from employees
  3  start with last_name='King'
  4  connect by prior employee_id=manager_id;

Walk T D
-----------------------------------------------------------
Kingreport to
Kingreport to
Kochharreport toKing
Greenbergreport toKochhar
Favietreport toGreenberg
Chenreport toGreenberg
Sciarrareport toGreenberg
Urmanreport toGreenberg
Poppreport toGreenberg
Whalenreport toKochhar
Mavrisreport toKochhar

Walk T D
-----------------------------------------------------------
Baerreport toKochhar
Higginsreport toKochhar

 

LEVEL和LPAD

示例:

SQL> select lpad(last_name,length(last_name)+(level*2)-2,'#')
  2  as org_chart
  3  from employees
  4  start with first_name='Steven' and last_name='King'
  5  connect by prior employee_id=manager_id;

ORG_CHART
--------------------------------------------------------------------------------
King
##Kochhar
####Greenberg
######Faviet
######Chen
######Sciarra
######Urman
######Popp
####Whalen
####Mavris
####Baer

 

 

Pruning Branches修剪问题

WHERE去掉一个节点,AND去掉整个枝干。

 

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-9-28 10:19 , Processed in 0.118088 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表