0

我有三个表看起来(只是一个例子)如下

Artifact 
Id Name     
1  abc
2  xyz
3  pqr
4  mno

Classification 
Id Artifact_Id Node_id 
c1 1             n3
c2 1             n4
c3 3             n5
c4 4             n3
c5 4             n2

taxonomy_Node
Id  Parent_id
n1  null
n2  n1
n3  n1
n4  n2
n5  n3

因此,每个工件都可以有许多分类(由分类节点组成的层次结构,即许多节点)。基于分类节点,我想要一个包含该节点或其子节点的工件列表。当它们具有属于 taxonomy_node 的分类时,我试图返回一个工件列表。此外,如果任何节点的父节点作为 ID 给出,它应该返回包含子节点的工件。我在这里不是很清楚。如果您有任何疑问,请告诉我。

select A.* from artifact A 
   inner join classification C 
    on A.id = C.ARTIFACT_ID
   inner join TAXONOMY_NODE T 
    on C.node_id=T.id
    where T.id = 5068
  START WITH T.ID = 5068
  CONNECT BY PRIOR T.ID = T.parent_id

例如,查看分类表工件 4 具有两个类 c4 和 c5,节点为 n2 和 n3。因此,当我将 node-id 作为 n3 时,它应该给我工件 4 和工件 1(因为它包含 n3)和工件 3(因为 n3 是 n5 的父级)。同样,当我将 node-id 作为 n2 时,它应该返回 4 和 1

4

1 回答 1

2
/*
with Artifact as (
select 1 id,  'abc' name from dual
union all select 2,  'xyz' from dual
union all select 3,  'pqr' from dual
union all select 4,  'mno' from dual
),
Classification as (
select 'c1' id, 1 Artifact_Id, 'n3' Node_id from dual
union all select 'c2', 1, 'n4' from dual
union all select 'c3', 3, 'n5' from dual
union all select 'c4', 4, 'n3' from dual
union all select 'c5', 4, 'n2' from dual
),
taxonomy_Node as (
select 'n1' id, null Parent_id from dual
union all select 'n2',  'n1' from dual
union all select 'n3',  'n1' from dual
union all select 'n4',  'n2' from dual
union all select 'n5',  'n3' from dual
)
*/
select a.* 
from classification c 
     join (select id from taxonomy_node start with id = 'n3' connect by nocycle prior id = parent_id) h
     on (c.node_id = h.id)
     join artifact a on (a.id = c.artifact_id);

您的查询的问题是 Oracle 先进行所有联接,然后再应用 CONNECT BY。

于 2014-10-21T20:05:57.757 回答