如何呈现id /层次查询树

时间:2015-11-26 13:14:40

标签: sql oracle hierarchical-data recursive-query

我与分层查询作斗争,但最后我不得不放弃。

我需要在输入上显示所有树/层次结构ID。

也许最好的方法是展示我想要做的例子:

CREATE TABLE ORDERS (ID NUMBER)
;
CREATE TABLE PRODUCTS (ID NUMBER,ORDER_ID NUMBER, PARENT_ID NUMBER)
;
CREATE TABLE WANT_TO_PRESENT (ID NUMBER);
INSERT INTO ORDERS VALUES (65733);
INSERT INTO ORDERS VALUES (23423);
INSERT INTO ORDERS VALUES (456765);
INSERT INTO ORDERS VALUES (23464);
INSERT INTO ORDERS VALUES (77532);
INSERT INTO PRODUCTS VALUES (1,65733,3);
INSERT INTO PRODUCTS VALUES (2,23423,3);
INSERT INTO PRODUCTS VALUES (3,77532,4);
INSERT INTO PRODUCTS VALUES (4,23464,0); 
INSERT INTO PRODUCTS VALUES (5,456765,null);

示例1:

INSERT INTO WANT_TO_PRESENT(ID) VALUES (23464) 

然后,由于parent_id = 0null

,输出应仅为23464

示例2:

INSERT INTO WANT_TO_PRESENT(ID) VALUES (23423)

然后输出应为:65733,23423,77532,23464

为什么65733 - 因为parent_id与23423相同。

示例3:

INSERT INTO WANT_TO_PRESENT(ID) VALUES (23464,65733) 

然后输出应该是:输出应该是:

65733,23423,77532,23464,65733 - 与上述相同

我想知道我是否真的需要表格命令来呈现树,尽管我在Products表中找到了所有数据。

你可以给我一些提示吗?一些解释,我想最终了解分层查询的工作原理......

编辑:

insert into products values (6,23422,7);
insert into products values (7,56435,0);
truncate table WANT_TO_PRESENT;
INSERT INTO WANT_TO_PRESENT(ID) VALUES (56435);

2 个答案:

答案 0 :(得分:1)

您必须合并两个查询:

首先爬到formFieldset.add(field); 表中父母ID的根目录。然后从这个根源到所有孩子们。

有一件事让它变得更加困难。产品4具有根父级0,但没有具有此密钥的记录。产品5是root并且父NULL。这在PRODUCT子句中处理,如果您使用唯一表示法,则可以简化。

START WITH

为您提供测试数据:

with to_root as (
select 
  t1.id,
  connect_by_root(t1.PARENT_ID) as root
from 
  products t1
where t1.id in (select id from PRODUCTS 
                 where ORDER_ID in (select id from WANT_TO_PRESENT))
start with nvl(t1.PARENT_ID,0) = 0
connect by t1.PARENT_ID = prior t1.id
),
go_down as (
select 
  t1.id,
  t1.parent_id
from 
  products t1
start with  PARENT_ID  in (select  root  from to_root) or  
                   ID in (select  ID  from to_root)
connect by   t1.PARENT_ID =  prior t1.id
)
--select * from to_root;
select ORDER_ID from PRODUCTS
where id in (
select id from go_down
)
order by 1
;

答案 1 :(得分:1)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE ORDERS (ID NUMBER PRIMARY KEY);
INSERT INTO ORDERS VALUES (65733);
INSERT INTO ORDERS VALUES (23423);
INSERT INTO ORDERS VALUES (456765);
INSERT INTO ORDERS VALUES (23464);
INSERT INTO ORDERS VALUES (77532);
insert into ORDERS values (23422);
insert into ORDERS values (56435);

CREATE TABLE PRODUCTS (
  ID NUMBER PRIMARY KEY,
  ORDER_ID NUMBER REFERENCES ORDERS(ID),
  PARENT_ID NUMBER
);
INSERT INTO PRODUCTS VALUES (1,65733,3);
INSERT INTO PRODUCTS VALUES (2,23423,3);
INSERT INTO PRODUCTS VALUES (3,77532,4);
INSERT INTO PRODUCTS VALUES (4,23464,0); 
INSERT INTO PRODUCTS VALUES (5,456765,null);
insert into products values (6,23422,7);
insert into products values (7,56435,0);

查询1

WITH WantToPresent( ID ) AS (
  SELECT 23464 FROM DUAL
)
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY ID = PRIOR parent_id
UNION
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY PRIOR ID = parent_id
UNION
SELECT p2.ORDER_ID
FROM   PRODUCTS p1
       INNER JOIN
       PRODUCTS p2
       ON ( p1.PARENT_ID = p2.PARENT_ID AND p2.PARENT_ID <> 0 )
       INNER JOIN
       WantToPresent w
       ON ( p1.ORDER_ID = w.ID )

<强> Results

| ORDER_ID |
|----------|
|    23423 |
|    23464 |
|    65733 |
|    77532 |

查询2

WITH WantToPresent( ID ) AS (
  SELECT 23423 FROM DUAL
)
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY 
  ID = PRIOR parent_id
UNION
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY PRIOR ID = parent_id
UNION
SELECT p2.ORDER_ID
FROM   PRODUCTS p1
       INNER JOIN
       PRODUCTS p2
       ON ( p1.PARENT_ID = p2.PARENT_ID AND p2.PARENT_ID <> 0 )
       INNER JOIN
       WantToPresent w
       ON ( p1.ORDER_ID = w.ID )

<强> Results

| ORDER_ID |
|----------|
|    23423 |
|    23464 |
|    65733 |
|    77532 |

查询3

WITH WantToPresent( ID ) AS (
  SELECT 23464 FROM DUAL UNION ALL
  SELECT 65733 FROM DUAL
)
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY 
  ID = PRIOR parent_id
UNION
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY PRIOR ID = parent_id
UNION
SELECT p2.ORDER_ID
FROM   PRODUCTS p1
       INNER JOIN
       PRODUCTS p2
       ON ( p1.PARENT_ID = p2.PARENT_ID AND p2.PARENT_ID <> 0 )
       INNER JOIN
       WantToPresent w
       ON ( p1.ORDER_ID = w.ID )

<强> Results

| ORDER_ID |
|----------|
|    23423 |
|    23464 |
|    65733 |
|    77532 |

查询4

WITH WantToPresent( ID ) AS (
  SELECT 56435 FROM DUAL
)
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY 
  ID = PRIOR parent_id
UNION
SELECT ORDER_ID
FROM   PRODUCTS p
START WITH
  EXISTS( SELECT 'X'
          FROM   WantToPresent w
          WHERE  p.ORDER_ID = w.ID )
CONNECT BY PRIOR ID = parent_id
UNION
SELECT p2.ORDER_ID
FROM   PRODUCTS p1
       INNER JOIN
       PRODUCTS p2
       ON ( p1.PARENT_ID = p2.PARENT_ID AND p2.PARENT_ID <> 0 )
       INNER JOIN
       WantToPresent w
       ON ( p1.ORDER_ID = w.ID )

<强> Results

| ORDER_ID |
|----------|
|    23422 |
|    56435 |