Oracle SQL 从下到上元素分层查询
Oracle SQL hierarchical query from bottom to top element
在我的 table 中,我存储了每个条目的后继者。
+----+-----------+--+
| ID | SUCCESSOR | |
+----+-----------+--+
| 1 | 2 | |
| 2 | 3 | |
| 3 | | |
+----+-----------+--+
我需要从 ID 3 到 ID 1.
我试图通过以下查询来实现这一点,但这不起作用。 :-(
SELECT NVL (id, 3)
FROM my_table
WHERE LEVEL = 1
CONNECT BY id = PRIOR successor
START WITH id = 3;
有人可以给我一些如何让它工作的建议吗?
以下版本也应该提供正确答案:
with my_table as
(select 1 id, 2 successor from dual union
select 2 id, 3 successor from dual union
select 3 id, null successor from dual )
SELECT id FROM my_table
WHERE level = 3
CONNECT BY successor = PRIOR id
START WITH successor is null
;
在我的 table 中,我存储了每个条目的后继者。
+----+-----------+--+ | ID | SUCCESSOR | | +----+-----------+--+ | 1 | 2 | | | 2 | 3 | | | 3 | | | +----+-----------+--+
我需要从 ID 3 到 ID 1.
我试图通过以下查询来实现这一点,但这不起作用。 :-(
SELECT NVL (id, 3)
FROM my_table
WHERE LEVEL = 1
CONNECT BY id = PRIOR successor
START WITH id = 3;
有人可以给我一些如何让它工作的建议吗?
以下版本也应该提供正确答案:
with my_table as
(select 1 id, 2 successor from dual union
select 2 id, 3 successor from dual union
select 3 id, null successor from dual )
SELECT id FROM my_table
WHERE level = 3
CONNECT BY successor = PRIOR id
START WITH successor is null
;