关系模型是
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这个解决方案非常接近我,但不适用于我的模型:
数据库版本 - 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
然后我必须改变方向向下..然后再次向上..然后再次向下..
但是如何在一次选择中联合所有这些步骤?初学者很难。请帮帮我。