从 Oracle 获取树/分层结构数据的最佳方法是什么

发布于 2024-11-01 00:00:55 字数 597 浏览 5 评论 0原文

我想从具有以下定义的表中获取如下所示的分层/树数据。

Tree Table:
"""""""""""
Id   |ParentId
"""""""""""
Work1|null
Work2|Work1
Work3|Work2
...

所需的查询结果数据(无需选项卡)- 如果我选择“Work1”,我应该完成位于其根目录下的 Id,如下所示。如果我选择“Work2”,那么我还应该填写其根目录上方和下方的 Id。

> Work1 
----------
>   Work2
----------
>     Work3
---------

我使用了下面的查询,但它给了我重复的记录。

select Id from TreeTable start with Id in ('Work1','Work2') connect by nocycle Parentid=prior Id or Id = prior Parentid

注意:我想以单一查询(优化)方式获取数据

谢谢和问候, 光伏发电。

I want to fetch Hierarchical/Tree data something like below from a Table which has following definiton.

Tree Table:
"""""""""""
Id   |ParentId
"""""""""""
Work1|null
Work2|Work1
Work3|Work2
...

Required Query result Data (no need to be tabbed)- If I Pick 'Work1' I should complete Ids which are under its root something like below. If I pick 'Work2' then also I should complete Ids above and below its root.

> Work1 
----------
>   Work2
----------
>     Work3
---------

I used below query but it is giving me duplicate records.

select Id from TreeTable start with Id in ('Work1','Work2') connect by nocycle Parentid=prior Id or Id = prior Parentid

Note: I want to get the data within a SINGLE QUERY (optimized) way

Thanks and Regards,
PV.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2024-11-08 00:00:55
SELECT  id
FROM    q
START WITH
        id IN ('Work1', 'Work2')
CONNECT BY
        parent = PRIOR id
        AND id NOT IN ('Work1', 'Work2')

此条件:

AND id NOT IN ('Work1', 'Work2')

将切断已使用 START WITH 选择的分支。

SELECT  id
FROM    q
START WITH
        id IN ('Work1', 'Work2')
CONNECT BY
        parent = PRIOR id
        AND id NOT IN ('Work1', 'Work2')

This condition:

AND id NOT IN ('Work1', 'Work2')

will cut off the branches already selected with START WITH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文