8

关系模型是

1   3   
 \ / \
  2   4
   \
    7   5     8
     \ /     /
      6     9

表是:

select 2 child, 1 father from dual
union all
select 2 child, 3 father from dual
union all
select 4 child, 3 father from dual
union all
select 7 child, 2 father from dual
union all
select 6 child, 5 father from dual
union all
select 6 child, 7 father from dual
union all
select 9 child, 8 father from dual

如何获得与值 CHILD 或 FATHER = 2 链接的所有值?

一定是

1,2,3,4,5,6,7

并不是

8,9

因为它与值 2 无关。

如何通过使用 CONNECT BY 语句来实现这一点?谢谢你。

ps这个解决方案非常接近我,但不适用于我的模型:

使用 oracle 连接查找邻接列表模型中的所有节点

数据库版本 - 10.2.0.5.0

模型与 Oracle 连接方式

因此,大致的策略可能是这样的(例如从 node=7 开始):

第 1 步(方向 = 向上)

select t1.father,connect_by_root father as root,connect_by_isleaf from 
(my_table) t1
start with father=7
connect by prior father = child 

结果是 7,2,1,3,其中 1,3 是高级根 (isleaf=1)

第 2 步(获取 1,3 方向=向下的路线)

select t1.child,connect_by_root father as root,connect_by_isleaf from 
(my_table) t1
start with father=1
connect by father = prior child 

结果是 2,7,6,其中 6 是低级根 (isleaf=1)

select t1.child,connect_by_root father as root,connect_by_isleaf from 
(my_table) t1
start with father=3
connect by father = prior child

结果是 2,7,6,4,其中 6,4 是低级根 (isleaf=1)

第 3 步(获取 6,4 方向的路线 = 向上)

select t1.father,connect_by_root father as root,connect_by_isleaf from 
(my_table) t1
start with child=6
connect by prior father = child 

结果是 5,7,2,1,3 其中 5,1,3 是高级根 (isleaf=1) 我发现这个结果是 node=5

然后我必须改变方向向下..然后再次向上..然后再次向下..

但是如何在一次选择中联合所有这些步骤?初学者很难。请帮帮我。

4

3 回答 3

3

对于您的输出,您不需要定向图表,因此将反向链接添加到所有现有链接。这就是我在子查询“bi”中所做的。然后您使用 nocyle 通过查询连接。

    with h as (
                     SELECT 2 child, 1 father FROM dual
                     UNION ALL
                     SELECT 2 child, 3 father FROM dual
                     UNION ALL
                     SELECT 4 child, 3 father FROM dual
                     UNION ALL
                     SELECT 7 child, 2 father FROM dual
                     UNION ALL
                     SELECT 6 child, 5 father FROM dual
                     UNION ALL
                     SELECT 6 child, 7 father FROM dual
                     UNION ALL
                     SELECT 9 child, 8 father FROM dual
            ),
    bi as (select * from h union all select father , child from h )     
    select distinct father from bi
    start with child = 2
    connect by nocycle
    prior father = child

为了更好的可读性,我对查询使用了“with”表示法。

于 2013-09-08T01:31:22.220 回答
0

这就是您所要求的:

SELECT     child
FROM       (
             SELECT 2 child, 1 father FROM dual
             UNION ALL
             SELECT 2 child, 3 father FROM dual
             UNION ALL
             SELECT 4 child, 3 father FROM dual
             UNION ALL
             SELECT 7 child, 2 father FROM dual
             UNION ALL
             SELECT 6 child, 5 father FROM dual
             UNION ALL
             SELECT 6 child, 7 father FROM dual
             UNION ALL
             SELECT 9 child, 8 father FROM dual
           )
START WITH father IN ( SELECT  father
                       FROM     
                       (
                                   SELECT 2 child, 1 father FROM dual
                                   UNION ALL
                                   SELECT 2 child, 3 father FROM dual
                                   UNION ALL
                                   SELECT 4 child, 3 father FROM dual
                                   UNION ALL
                                   SELECT 7 child, 2 father FROM dual
                                   UNION ALL
                                   SELECT 6 child, 5 father FROM dual
                                   UNION ALL
                                   SELECT 6 child, 7 father FROM dual
                                   UNION ALL
                                   SELECT 9 child, 8 father FROM dual
                        )
                       START WITH child = 2
                       CONNECT BY PRIOR father = child)
CONNECT BY PRIOR child = father
UNION
SELECT     father
FROM       (
SELECT 2 child, 1 father FROM dual
UNION ALL
SELECT 2 child, 3 father FROM dual
UNION ALL
SELECT 4 child, 3 father FROM dual
UNION ALL
SELECT 7 child, 2 father FROM dual
UNION ALL
SELECT 6 child, 5 father FROM dual
UNION ALL
SELECT 6 child, 7 father FROM dual
UNION ALL
SELECT 9 child, 8 father FROM dual
          )
START WITH child IN (
SELECT     child
FROM       (
             SELECT 2 child, 1 father FROM dual
             UNION ALL
             SELECT 2 child, 3 father FROM dual
             UNION ALL
             SELECT 4 child, 3 father FROM dual
             UNION ALL
             SELECT 7 child, 2 father FROM dual
             UNION ALL
             SELECT 6 child, 5 father FROM dual
             UNION ALL
             SELECT 6 child, 7 father FROM dual
             UNION ALL
             SELECT 9 child, 8 father FROM dual
           )
        START WITH child = 2
          CONNECT BY PRIOR child = father)


CONNECT BY PRIOR father = child;

此查询包含两个块。第一个块提取 2 的所有父亲的所有孩子(参数),第二个块(使用联合以避免重复)提取 2 的所有孩子的所有父亲。

于 2013-05-10T20:40:33.353 回答
0

如果我理解正确,您可以使用如下的 connect_by_root 函数:

select hier.child , hier.father
from 
(
  select t.* , connect_by_root(father) top_father
  from 
  (
    select 2 child, 1 father from dual
    union all
    select 2 child, 3 father from dual
    union all
    select 4 child, 3 father from dual
    union all
    select 7 child, 2 father from dual
    union all
    select 6 child, 5 father from dual
    union all
    select 6 child, 7 father from dual
    union all
    select 9 child, 8 father from dual
  ) t
  connect by t.child = prior t.father
) hier
where 2 in (hier.child , hier.top_father);
于 2013-07-28T12:23:01.110 回答