如何在Oracle SQL中选择相关的项目组 [英] How to select a related group of items in Oracle SQL

查看:70
本文介绍了如何在Oracle SQL中选择相关的项目组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些形式的数据

Key    ID      Link       
1      MASTER  123
2      AA      123
3      AA      123
4      BB      123
5      MASTER  456
6      CC      456

我希望能够在同一选择中选择所有符合选择标准的链接项目,以及链接的主文件.例如,如果我的ID为"AA",我希望返回ID ="AA"的行,以及ID ="MASTER"和链接123的行:

I would like to be able to select in the same select all linked items matching the selection criteria, plus the linked master. For example, if I have an ID of 'AA', I want the rows with ID = 'AA' to be returned, plus the row with ID = 'MASTER' and a link of 123:

1      MASTER  123
2      AA      123
3      AA      123

我正在使用Oracle 10.2g,因此,如果有任何特殊的Oracle语法可以简化此操作,那就可以了.

I'm using Oracle 10.2g, so if any special Oracle syntax will make this easier, then that would be ok.

推荐答案

这里是一种方法.

SELECT DISTINCT key, id, link
  FROM the_table
  START WITH id = 'AA'
  CONNECT BY id = 'MASTER' and link = PRIOR link and 'AA' = PRIOR ID

这篇关于如何在Oracle SQL中选择相关的项目组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆