检索给定记录的所有孩子和父母

Posted

技术标签:

【中文标题】检索给定记录的所有孩子和父母【英文标题】:Retrieve all children and parents of a given record 【发布时间】:2014-06-29 12:08:23 【问题描述】:

我想检索给定记录的所有父记录,然后获取这些父记录的所有子记录。

例如:如果表格是这样的,

表 1:

Child_Id  | Parent_Id
---------------------
    23           4
    23           5
    4            20
    20           21
    5            12
    12           15
    12           17
    24           30
    39            4

鉴于上面的表格和 id 是 23 那么我必须检索,

21
   20 
      4 
        23  39
15
   12
      5
        23

17 
   12
      5
        23

我之前尝试过连接,但它对我有帮助,

SELECT * FROM TABLE1 
CONNECT BY PRIOR CHILD_ID = PARENT_ID

有没有办法先获取给定节点的所有父节点,然后获取其父节点的所有子节点以及兄弟节点?

【问题讨论】:

您使用的是什么版本的 Oracle? 。 .太糟糕了。 Oracle 11g 具有递归 CTE,我发现它比 connect by 更易于使用。 【参考方案1】:

您可以使用以下方法获取父母/根的列表:

select parent_id as parent
  from tbl x
 where not exists (select 1 from tbl y where y.child_id = x.parent_id)

| PARENT |
|--------|
|     15 |
|     21 |
|     17 |
|     30 |

http://sqlfiddle.com/#!4/62c37/18/0

然后对于给定的根,您可以运行以下命令:

select 21 as parent,
       listagg(child_id, ' >>> ') within group(order by level) as children
  from tbl
 start with parent_id = 21
connect by prior child_id = parent_id


| PARENT |               CHILDREN |
|--------|------------------------|
|     21 | 20 >>> 4 >>> 23 >>> 39 |

http://sqlfiddle.com/#!4/62c37/35/0

或者,如果您不想让孩子们排成一排,您可以通过运行获得类似于您给出的输出:

select 21 as parent,
       lpad(child_id,level*level,' ') as child
  from tbl
 start with parent_id = 21
connect by prior child_id = parent_id


| PARENT |     CHILD |
|--------|-----------|
|     21 |         2 |
|     21 |         4 |
|     21 |        23 |
|     21 |        39 |

http://sqlfiddle.com/#!4/62c37/34/0

我不确定是否在一个查询中为所有根运行它,但我想我至少会给你那个等待另一个答案。

但是,如果每个孩子总是只有几个孩子(例如,10 岁以下),那么这应该是可行的,无需依赖 CONNECT BY,而是使用子查询。

作为该方法的一个示例,以下内容有 8 个级别,并且只要不超过 8 个子级,就应该适用于整个表(如果不是这种情况,您可以添加更多的子级):

with lvl1 as
 (select x.parent_id, x.child_id
    from tbl x
   where not exists (select 1 from tbl y where y.child_id = x.parent_id)),
lvl2 as
 (select x.parent_id, x.child_id
    from tbl x
    left join lvl1
      on x.parent_id = lvl1.child_id),
lvl3 as
 (select x.parent_id, x.child_id
    from tbl x
    left join lvl2
      on x.parent_id = lvl2.child_id),
lvl4 as
 (select x.parent_id, x.child_id
    from tbl x
    left join lvl3
      on x.parent_id = lvl3.child_id),
lvl5 as
 (select x.parent_id, x.child_id
    from tbl x
    left join lvl4
      on x.parent_id = lvl4.child_id),
lvl6 as
 (select x.parent_id, x.child_id
    from tbl x
    left join lvl5
      on x.parent_id = lvl5.child_id),
lvl7 as
 (select x.parent_id, x.child_id
    from tbl x
    left join lvl6
      on x.parent_id = lvl6.child_id),
lvl8 as
 (select x.parent_id, x.child_id
    from tbl x
    left join lvl7
      on x.parent_id = lvl7.child_id)
select  parent_id,
        case when lag(child2,1) over (partition by parent_id order by parent_id) = child2 then null else child2 end as child2,
        case when lag(child3,1) over (partition by parent_id order by parent_id) = child3 then null else child3 end as child3,
        case when lag(child4,1) over (partition by parent_id order by parent_id) = child4 then null else child4 end as child4,
        case when lag(child5,1) over (partition by parent_id order by parent_id) = child5 then null else child5 end as child5,
        case when lag(child6,1) over (partition by parent_id order by parent_id) = child6 then null else child6 end as child6,
        case when lag(child7,1) over (partition by parent_id order by parent_id) = child7 then null else child7 end as child7,
        case when lag(child8,1) over (partition by parent_id order by parent_id) = child8 then null else child8 end as child8
from(
select distinct
       lvl1.parent_id,
       lvl2.parent_id as child2,
       nvl(lvl3.parent_id,lvl2.child_id) as child3,
       nvl(lvl4.parent_id,lvl3.child_id) as child4,
       nvl(lvl5.parent_id,lvl4.child_id) as child5,
       nvl(lvl6.parent_id,lvl5.child_id) as child6,
       nvl(lvl7.parent_id,lvl6.child_id) as child7,
       nvl(lvl8.parent_id,lvl7.child_id) as child8
  from lvl1
  left join lvl2
    on lvl1.child_id = lvl2.parent_id
  left join lvl3
    on lvl2.child_id = lvl3.parent_id
  left join lvl4
    on lvl3.child_id = lvl4.parent_id
  left join lvl5
    on lvl4.child_id = lvl5.parent_id
  left join lvl6
    on lvl5.child_id = lvl6.parent_id
  left join lvl7
    on lvl6.child_id = lvl7.parent_id
  left join lvl8
    on lvl7.child_id = lvl8.parent_id
order by parent_id)

http://sqlfiddle.com/#!4/62c37/50/0

| PARENT_ID | CHILD2 | CHILD3 | CHILD4 | CHILD5 | CHILD6 | CHILD7 | CHILD8 |
|-----------|--------|--------|--------|--------|--------|--------|--------|
|        15 |     12 |      5 |     23 | (null) | (null) | (null) | (null) |
|        17 |     12 |      5 |     23 | (null) | (null) | (null) | (null) |
|        21 |     20 |      4 |     23 | (null) | (null) | (null) | (null) |
|        21 | (null) | (null) |     39 | (null) | (null) | (null) | (null) |
|        30 | (null) | (null) | (null) | (null) | (null) | (null) | (null) |

【讨论】:

以上是关于检索给定记录的所有孩子和父母的主要内容,如果未能解决你的问题,请参考以下文章

使用查询检查记录

如何使用 JPA/Hibernate 在孩子的 id 中引用父母的 id?

NSPredicate 检索所有具有特定孩子的父母? (IOS)

Mysql如何获取给定ID的父母和所有孩子? [复制]

让所有父母为孩子

从某些父母那里退回那些他们的孩子的某些财产等于某个价值的记录?