【问题标题】:Get all parents for given element and their siblings获取给定元素的所有父母及其兄弟姐妹
【发布时间】:2014-11-05 17:28:12
【问题描述】:

我得到了一些带有层次结构的表:

create table t_hier (id number primary key, parent number);
insert into t_hier (id, parent) values(0, null);
insert into t_hier (id, parent) values(1, 0);
insert into t_hier (id, parent) values(2, 0);
insert into t_hier (id, parent) values(3, 1);
insert into t_hier (id, parent) values(4, 1);
insert into t_hier (id, parent) values(5, 2);
insert into t_hier (id, parent) values(6, 2);
insert into t_hier (id, parent) values(7, 5);
insert into t_hier (id, parent) values(8, 5);

select rpad('* ', 2*level, '* ')||id id, parent
from t_hier
connect by prior id = parent
start with parent is null;

ID                   PARENT
____________________ ______
* 0 
* * 1                0
* * * 3              1
* * * 4              1
* * 2                0
* * * 5              2
* * * * 7            5
* * * * 8            5
* * * 6              2

给定一些 ID,我需要获取它的所有父母、祖父母等,以及返回元素的每个兄弟姐妹(我的兄弟姐妹是指仅具有相同父级的元素,而不是整个级别),还需要给定元素本身。
因此,如果我有 id 为 5 的元素,我需要返回 0、1、2、5 和 6。
对于 id 为 7 的元素,我需要返回 0、1、2、5、6、7、8。

我认为只需一个查询即可完成,如果有人能帮助我,那就太好了。

【问题讨论】:

    标签: sql oracle hierarchy


    【解决方案1】:
    with parents as (
      select level lvl, id
      from t_hier
      start with id = 7
      connect by id = prior parent
    )
    select distinct id from t_hier
    where id != 7
    start with id in (select id from parents where lvl > 1)
    connect by prior id = parent and level <= 2;
    
    1. 找到所有的祖先
    2. 回去为每个祖先找到他的孩子,但只在第二层
    3. 排除起始ID。

    【讨论】:

    • 为什么不只是select id from t_hier where parent in ( select parent from t_hier connect by prior parent = id start with id = 7 ) or parent is null?它似乎也有效
    • @SpongeBobFan 是的,我同意,因为您只需要一个级别。您的变体更简单。
    【解决方案2】:

    这可能会有所帮助:

    with cte_getHierarchy (id,parent)
    as 
    (
      select t_hier.id,t_hier.parent from t_hier where id = 7
      union all
      select t_hier.id,t_hier.parent from t_hier join cte_getHierarchy on t_hier.id = cte_getHierarchy.parent
    ),
    cte_getsibling (id,parent) 
    as 
    (
      select cte_getHierarchy.id,cte_getHierarchy.parent from cte_getHierarchy 
      union
      select t_hier.id,t_hier.parent from t_hier join cte_getHierarchy on  t_hier.parent = cte_getHierarchy.parent
    
    )
    select id from cte_getsibling where id <> 7;
    

    sql fiddle

    【讨论】:

      【解决方案3】:

      应该是这个:

      select rpad('* ', 2*level, '* ')||id id, parent
      from t_hier
      connect by id = prior parent
      start with id = 3
      union 
      select rpad('* ', 2*level, '* ')||id id, parent
      from t_hier
      connect by prior id = parent and level = 1
      start with parent = (select parent from t_hier where id = 3);
      

      为什么 id 7 应该返回 0, 1(???), 2, 5, 6, 8。 1 既不是 7 的祖先,也不是兄弟姐妹。

      【讨论】:

      • 感谢您的帮助!我需要为结果集中的每个元素返回兄弟姐妹,这就是为什么查询 id=7 应该返回 id=1 的元素
      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多