Oracle   发布时间:2022-05-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用Oracle CONNECT BY将层次结构中的所有值链接到某个值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
关系模型是
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语句来实现这一点?谢谢.

附:这个解决方案离我很近但不适用于我的模型:

Find all nodes in an adjacency list model with Oracle connect by

数据库版本 – 10.2.0.5.0

模型与甲骨文 – 连接 – 通过

所以,大概的策略可能是这样的(例如从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,1,其中1,3是高级根(isleaf = 1)

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

SELEct t1.child,connecT_By_isleaf from 
(my_tablE) t1
start with father=1
connect by father = prior child

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

SELEct t1.child,connecT_By_isleaf from 
(my_tablE) t1
start with father=3
connect by father = prior child

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

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

SELEct t1.father,connecT_By_isleaf from 
(my_tablE) t1
start with child=6
connect by prior father = child

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

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

但如何在一个选择中结合所有这些步骤?对于初学者来说很难.请帮帮我.

对于您的输出,您不需要定向图表,因此请将反向链接添加到所有现有链接.这就是我在子查询’bi’中所做的.然后通过查询使用Nocyle connect.
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’表示法以获得更好的可读性.

大佬总结

以上是大佬教程为你收集整理的如何使用Oracle CONNECT BY将层次结构中的所有值链接到某个值全部内容,希望文章能够帮你解决如何使用Oracle CONNECT BY将层次结构中的所有值链接到某个值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: