Oracle-SQL单select,按字段级联
Oracle-SQL Single select, cascade by field
我不太确定如何表达我的意思。让我试试看:有没有办法 select 由参考字段级联的所有元素?
例如,我有以下行:
parentRef | Reference | Data
------------------------------
aContainer | mainObj | "Parent"
mainObj | secondObj | "Child 1"
secondObj | thirdObj | "Child 2"
nonExistent | blankObj | "Don't select me!"
当我只知道一个 parentRef:"aContainer" 时,我想在一条语句中 select mainObj、secondObj、thirdObj。这可能吗?
我可以通过让我的代码执行很多查询来做到这一点:select...where parentRef = 'aContainer'
,然后 select...where parentRef = 'mainObj'
,等等,但我真的不想用很多查询来打击我的数据库,主要是为了速度.
编辑:树查询!这就是我需要的搜索词。
Oracle 可以执行树查询,查看 START WITH 和 CONNECT BY
如果我没理解错的话,您想要的是相关查询之类的东西。这将允许您仅获取在 table 中具有父引用的项目。一个示例如下所示(尽管我可能颠倒了逻辑):
select
parentRef
, Reference
, Data
from mytable parentTable
where Reference in (
select
reference
from mytable childTable
where childTable.reference = parenttable.parentref)
我不太确定如何表达我的意思。让我试试看:有没有办法 select 由参考字段级联的所有元素?
例如,我有以下行:
parentRef | Reference | Data ------------------------------ aContainer | mainObj | "Parent" mainObj | secondObj | "Child 1" secondObj | thirdObj | "Child 2" nonExistent | blankObj | "Don't select me!"
当我只知道一个 parentRef:"aContainer" 时,我想在一条语句中 select mainObj、secondObj、thirdObj。这可能吗?
我可以通过让我的代码执行很多查询来做到这一点:select...where parentRef = 'aContainer'
,然后 select...where parentRef = 'mainObj'
,等等,但我真的不想用很多查询来打击我的数据库,主要是为了速度.
编辑:树查询!这就是我需要的搜索词。
Oracle 可以执行树查询,查看 START WITH 和 CONNECT BY
如果我没理解错的话,您想要的是相关查询之类的东西。这将允许您仅获取在 table 中具有父引用的项目。一个示例如下所示(尽管我可能颠倒了逻辑):
select
parentRef
, Reference
, Data
from mytable parentTable
where Reference in (
select
reference
from mytable childTable
where childTable.reference = parenttable.parentref)