我有一个简单的父/子类型视图,其中包含两列:MYID
和MYPARENTID
。在PL / SQL中,获取父项的所有子项列表非常简单:
SELECT MYID
FROM MYVIEW
START WITH MYID = 'TargetId1'
CONNECT BY PRIOR MYID = MYPARENTID
我会得到这样的回复:
MYID
-----------
TargetId1
TargetId1Child1
TargetId1Grandchild1
但现在让我们说我想为一组父母做这件事,一下子:
SELECT MYID
FROM MYVIEW
START WITH MYID IN ('TargetId1', 'TargetId2', 'TargetId3')
CONNECT BY PRIOR MYID = MYPARENTID
我的结果如下:
MYID
---------
TargetId1
TargetId1Child1
TargetId1Grandchild1
TargetId2
TargetId2Child2
TargetId2Grandchild1
TargetId3
TargetId3Child3
TargetId3Grandchild1
当我这样做时,我失去了知道特定子节点来自何处的能力。我找回了一个孩子的清单,但我想知道每个孩子来自哪个根(基本上是START WITH
值);
我想要一个如下所示的结果集:
MYID ROOT
----------------------------------
TargetId1 TargetId1
TargetId1Child1 TargetId1
TargetId1Grandchild1 TargetId1
TargetId2 TargetId2
TargetId2Child2 TargetId2
TargetId2Grandchild2 TargetId2
TargetId3 TargetId3
TargetId3Child3 TargetId3
TargetId3Grandchild3 TargetId3
我该怎么做?
答案 0 :(得分:1)
我还没有用过它,但是根据我对你的问题的理解,我认为操作员CONNECT_BY_ROOT就是你要找的。但是需要Oracle 10g。
SELECT MYID
,CONNECT_BY_ROOT MYID ROOT
FROM MYVIEW
START WITH MYID IN ('TargetId1', 'TargetId2', 'TargetId3')
CONNECT BY PRIOR MYID = MYPARENTID
答案 1 :(得分:0)
我不知道PL / SQL,但您可以:
E.g。
CREATE TABLE #Parents (TargetId)
INSERT INTO #Parents TargetId1
INSERT INTO #Parents TargetId2
...
INSERT INTO #Parents TargetId10
SELECT MYID, #Parents.TargetID
FROM MYVIEW, #Parents
CONNECT BY MYID = #Parents.TargetID
CONNECT BY PRIOR MYID = MYPARENTID