sql - 在 oracle 树查询中加入其他表

标签 sql oracle tree connect-by

给定一个简单的 (id, description) 表 t1,例如

id  description
--  -----------
1   Alice
2   Bob
3   Carol
4   David
5   Erica
6   Fred

还有一个父子关系表t2,比如
parent  child
------  -----
1       2
1       3
4       5
5       6

Oracle 提供了一种将其作为具有一些自定义语法扩展的树来遍历的方法:
select parent, child, sys_connect_by_path(child, '/') as "path"
from t2
connect by prior parent = child

确切的语法并不重要,我可能在上面犯了一个错误。这
重要的是,上面会产生一些看起来像
parent  child  path
------  -----  ----
1       2      /1/2
1       3      /1/3
4       5      /4/5
4       6      /4/5/6
5       6      /5/6

我的问题是:是否可以在 sys_connect_by_path() 中加入另一个表,例如上面的 t1 表,以生成如下内容:
parent  child  path
------  -----  ----
1       2      /Alice/Bob
1       3      /Alice/Carol
... and so on...

最佳答案

在您的查询中,将 T2 替换为连接 T1 和 T2 的子查询,并返回父、子和子描述。然后在 sys_connect_by_path 函数中,从您的子查询中引用子描述。

关于sql - 在 oracle 树查询中加入其他表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/117512/

相关文章:

c - 如何用C语言制作可遍历的树数据结构

algorithm - 平衡四叉树

java - PostgreSQL 异常 : syntax error

sql - 使用sql检测循环

sql - Athena/SQL 查询以获得所需结果

oracle - oracle中静态游标与动态游标的区别

sql - Oracle通过db link查询丢失索引并执行全表扫描

sql - Oracle:检查其他表中是否存在行

sql - Oracle SQL 获取与 max(count()) 关联的列值

javascript - 遍历树状数组