我有三张表(只是一个例子),如下所示
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
因此,每个工件都可以有许多分类(由taxonomyNode构成的层次结构,即许多节点)。基于taxonomyNode我想要一个包含该节点或其子节点的工件列表。 当他们具有属于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
答案 0 :(得分: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。