我想要i节点我在start with子句中传递的所有节点

时间:2018-02-07 11:06:58

标签: database oracle hierarchy hierarchical-data sql-query-store

假设以下是层次结构: enter image description here

我正在通过 5 ,所以我除了5,4,2,1以外我该怎么办呢。 提前致谢。

1 个答案:

答案 0 :(得分:0)

如果您想要5的所有祖先和兄弟姐妹,请在start with子句中使用父ID:

/* sample data
with t(id, pid) as (
    select 1, null from dual union all
    select 2,    1 from dual union all
    select 3,    1 from dual union all
    select 4,    2 from dual union all
    select 5,    2 from dual union all
    select 6,    3 from dual ) */ 
select distinct id
  from t connect by prior pid = id
  start with pid = (select pid from t where id = 5)