自引用表上的 SQL 递归查询 (Oracle)
假设我有这个示例数据:
| Name | ID | PARENT_ID |
-----------------------------
| a1 | 1 | null |
| b2 | 2 | null |
| c3 | 3 | null |
| a1.d4 | 4 | 1 |
| a1.e5 | 5 | 1 |
| a1.d4.f6 | 6 | 4 |
| a1.d4.g7 | 7 | 4 |
| a1.e5.h8 | 8 | 5 |
| a2.i9 | 9 | 2 |
| a2.i9.j10| 10 | 9 |
我想选择从 accountId = 1 开始的所有记录,因此预期结果是:
| Name | ID | PARENT_NAME | PARENT_ID |
-------------------------------------------
| a1 | 1 | null | null |
| a1.d4 | 4 | a1 | 1 |
| a1.e5 | 5 | a1 | 1 |
| a1.d4.f6 | 6 | a1.d4 | 4 |
| a1.d4.g7 | 7 | a1.d4 | 4 |
| a1.e5.h8 | 8 | a1.e5 | 5 |
我当前能够进行递归选择,但随后我无法访问父级的数据引用,因此我无法返回parent_name。我使用的代码是(适用于简单的示例):
SELECT id, parent_id, name
FROM tbl
START WITH id = 1
CONNECT BY PRIOR id = parent_id
我应该使用什么 SQL 来进行上述检索?
供未来搜索者使用的其他关键字:用于选择由同一表中的父键表示的分层数据的 SQL
Lets assume I have this sample data:
| Name | ID | PARENT_ID |
-----------------------------
| a1 | 1 | null |
| b2 | 2 | null |
| c3 | 3 | null |
| a1.d4 | 4 | 1 |
| a1.e5 | 5 | 1 |
| a1.d4.f6 | 6 | 4 |
| a1.d4.g7 | 7 | 4 |
| a1.e5.h8 | 8 | 5 |
| a2.i9 | 9 | 2 |
| a2.i9.j10| 10 | 9 |
I would like to select all records start from accountId = 1, so the expected result would be:
| Name | ID | PARENT_NAME | PARENT_ID |
-------------------------------------------
| a1 | 1 | null | null |
| a1.d4 | 4 | a1 | 1 |
| a1.e5 | 5 | a1 | 1 |
| a1.d4.f6 | 6 | a1.d4 | 4 |
| a1.d4.g7 | 7 | a1.d4 | 4 |
| a1.e5.h8 | 8 | a1.e5 | 5 |
I am currently able to make the recursive select, but then I can't access the data from the parent reference, hence I can't return parent_name. The code I'm using is (adapted to the simplistic example):
SELECT id, parent_id, name
FROM tbl
START WITH id = 1
CONNECT BY PRIOR id = parent_id
What SQL should I be using to the mentioned above retrieval?
Additional key words for future seekers: SQL to select hierarchical data represented by parent keys in same table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用:
Use:
使用 PRIOR 怎么样,
所以
或者如果你想获取根名称
What about using PRIOR,
so
or if you want to get the root name
使用新的嵌套查询语法
Using the new nested query syntax
你想这样做吗?
编辑
另一种基于 OMG 的选项(但我认为效果相同):
Do you want to do this?
Edit
Another option based on OMG's one (but I think that will perform equally):
这有点麻烦,但我相信这应该可行(无需额外的连接)。这假设您可以选择一个永远不会出现在相关字段中的字符来充当分隔符。
您可以在不嵌套选择的情况下完成此操作,但我发现这更干净一点,有四个对 SYS_CONNECT_BY_PATH 的引用。
It's a little on the cumbersome side, but I believe this should work (without the extra join). This assumes that you can choose a character that will never appear in the field in question, to act as a separator.
You can do it without nesting the select, but I find this a little cleaner that having four references to SYS_CONNECT_BY_PATH.