oracle 9i 获取具有给定孩子的树的最高成员

发布于 2024-08-05 17:29:58 字数 229 浏览 9 评论 0原文

我在 Oracle 9i 数据库表中有一个父子关系,

如下所示:

parent | child  
1      | 2  
2      | 3
2      | 4
null   | 1
1      | 8

我需要从给定子项获取绝对父项。 假设我有孩子 4,它必须给我父母: 1

我已经查看过 CONNECT BY ,但我找不到解决方案。

I have a parent-child relationship in an Oracle 9i database-table

like:

parent | child  
1      | 2  
2      | 3
2      | 4
null   | 1
1      | 8

I need to get the absolute parent from a given child.
Say, I have child 4, it has to give me parent: 1

I already looked to CONNECT BY , but I can't find the solution.

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

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

发布评论

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

评论(2

狼性发作 2024-08-12 17:29:58

您可以使用 CONNECT BY 查询来构建父母列表,然后进行过滤:

SQL> WITH tree AS (
  2     SELECT 1 parent_id, 2 child_id FROM DUAL
  3     UNION ALL SELECT 2   , 3  FROM DUAL
  4     UNION ALL SELECT 2   , 4  FROM DUAL
  5     UNION ALL SELECT null, 1  FROM DUAL
  6     UNION ALL SELECT 1   , 8  FROM DUAL
  7  )
  8  SELECT child_id
  9    FROM (SELECT *
 10            FROM tree
 11          CONNECT BY PRIOR parent_id = child_id
 12           START WITH child_id = 4)
 13   WHERE parent_id IS NULL;

  CHILD_ID
----------
         1

you could use a CONNECT BY query to build the list of parents and then filter :

SQL> WITH tree AS (
  2     SELECT 1 parent_id, 2 child_id FROM DUAL
  3     UNION ALL SELECT 2   , 3  FROM DUAL
  4     UNION ALL SELECT 2   , 4  FROM DUAL
  5     UNION ALL SELECT null, 1  FROM DUAL
  6     UNION ALL SELECT 1   , 8  FROM DUAL
  7  )
  8  SELECT child_id
  9    FROM (SELECT *
 10            FROM tree
 11          CONNECT BY PRIOR parent_id = child_id
 12           START WITH child_id = 4)
 13   WHERE parent_id IS NULL;

  CHILD_ID
----------
         1
孤独陪着我 2024-08-12 17:29:58
SELECT  parent
FROM    (
        SELECT  parent
        FROM    (
                SELECT  parent, level AS l
                FROM    mytable
                START WITH
                        child = 4
                CONNECT BY
                        child = PRIOR parent
                )
        ORDER BY
                l DESC
        )
WHERE   rownum = 1

这将为您提供 NULL 作为绝对父级。

如果您想要 1,请将 parent 替换为 child

SELECT  child
FROM    (
        SELECT  child
        FROM    (
                SELECT  child, level AS l
                FROM    mytable
                START WITH
                        child = 4
                CONNECT BY
                        child = PRIOR parent
                )
        ORDER BY
                l DESC
        )
WHERE   rownum = 1
SELECT  parent
FROM    (
        SELECT  parent
        FROM    (
                SELECT  parent, level AS l
                FROM    mytable
                START WITH
                        child = 4
                CONNECT BY
                        child = PRIOR parent
                )
        ORDER BY
                l DESC
        )
WHERE   rownum = 1

This will give you NULL as the absolute parent.

If you want 1, replace parent with child:

SELECT  child
FROM    (
        SELECT  child
        FROM    (
                SELECT  child, level AS l
                FROM    mytable
                START WITH
                        child = 4
                CONNECT BY
                        child = PRIOR parent
                )
        ORDER BY
                l DESC
        )
WHERE   rownum = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文