如何获取父子层次结构

时间:2018-02-26 08:40:49

标签: oracle oracle11g

当我们通过' NAME'时,如何获取父子层次结构? as' 9780748700585'在TEMP_CHILD_ASSOC表中?TEMP_CHILD_ASSOC和TEMP_NODE表之间的关系是CHILD_ID是外键,ID是TEMP_NODE中的主键。

最终出局应该是

('1202','1223','9781424005468_p01_covpdf.zip');
('1202','1226','9781424005468_p01_hipdf.zip');
('1202','1229','9781424005468_p01_pod.zip');
('1202','1232','9781424005468_p01_pod_chaitu.zip');


 CREATE TABLE "TEMP_CHILD_ASSOC" ("PARENT_ID" VARCHAR2(20), "CHILD_ID" VARCHAR2(20), "NAME" VARCHAR2(2000))
REM INSERTING into TEMP_CHILD_ASSOC
SET DEFINE OFF;
Insert into TEMP_CHILD_ASSOC (PARENT_ID,CHILD_ID,NAME) values ('1141','1143','9780748700585');
Insert into TEMP_CHILD_ASSOC (PARENT_ID,CHILD_ID,NAME) values ('1143','1202','testfolder1');
Insert into TEMP_CHILD_ASSOC (PARENT_ID,CHILD_ID,NAME) values ('1202','1223','9781424005468_p01_covpdf.zip');
Insert into TEMP_CHILD_ASSOC (PARENT_ID,CHILD_ID,NAME) values ('1202','1226','9781424005468_p01_hipdf.zip');
Insert into TEMP_CHILD_ASSOC (PARENT_ID,CHILD_ID,NAME) values ('1202','1229','9781424005468_p01_pod.zip');
Insert into TEMP_CHILD_ASSOC (PARENT_ID,CHILD_ID,NAME) values ('1202','1232','9781424005468_p01_pod_chaitu.zip');


 CREATE TABLE "TEMP_NODE" ("ID" VARCHAR2(20), "NAME" VARCHAR2(20))
REM INSERTING into TEMP_NODE
SET DEFINE OFF;
Insert into TEMP_NODE (ID,NAME) values ('1143','product');
Insert into TEMP_NODE (ID,NAME) values ('1202','folder');
Insert into TEMP_NODE (ID,NAME) values ('1223','asset');
Insert into TEMP_NODE (ID,NAME) values ('1226','asset');
Insert into TEMP_NODE (ID,NAME) values ('1229','asset');
Insert into TEMP_NODE (ID,NAME) values ('1232','asset');

1 个答案:

答案 0 :(得分:0)

此查询生成您想要的结果:

SQL> select c.parent_id, c.child_id, c.name
  2  from temp_child_assoc c
  3  where level = 3
  4  connect by prior c.child_id = c.parent_id
  5  start with c.name = '9780748700585'
  6  order siblings by c.child_id;

PARENT_ID  CHILD_ID   NAME
---------- ---------- -----------------------------------
1202       1223       9781424005468_p01_covpdf.zip
1202       1226       9781424005468_p01_hipdf.zip
1202       1229       9781424005468_p01_pod.zip
1202       1232       9781424005468_p01_pod_chaitu.zip

SQL>

但是,我不明白TEMP_NODE表的目的是什么(因为我们不使用它),以及为什么你不想要CHILD_ID的1143 (第1级)和第1202级(第2级)。

[编辑,阅读评论后]

啊哈,你不想要文件夹。你为什么不这么说?

如果我们加入这些表(以便我们可以限制文件夹上的返回数据)并删除LEVEL条件,这就是你得到的:

SQL> select level, c.parent_id, c.child_id, c.name
  2  from temp_child_assoc c join temp_node n on c.child_id = n.id
  3  where n.name <> 'folder'
  4  connect by prior c.child_id = c.parent_id
  5  start with c.name = '9780748700585'
  6  order siblings by c.child_id;

     LEVEL PARENT_ID  CHILD_ID   NAME
---------- ---------- ---------- -----------------------------------
         1 1141       1143       9780748700585
         3 1202       1223       9781424005468_p01_covpdf.zip
         3 1202       1226       9781424005468_p01_hipdf.zip
         3 1202       1229       9781424005468_p01_pod.zip
         3 1202       1232       9781424005468_p01_pod_chaitu.zip

SQL>

第一行是你不需要的那一行。为什么?这不是文件夹,而是产品,所以......我担心我还是不了解这个要求。