一对多连接在同一张表的 3 个级别上

发布于 2024-09-11 06:09:41 字数 2260 浏览 5 评论 0原文

我有这个带有页面的表,这些页面的父级也是同一个表中的页面。

对于这个例子来说,表格看起来像:

table: Pages
PageId :Key
PageParent :Foreign Key
PageName

现在我的问题是,当创建一个像这样的菜单结构时,SQL 会是什么样子:

PageId     PageParent     PageName
1          NULL           home
 2          1              page_under_home1
  5          2              page_under_pageid2_1
  6          2              page_under_pageid2_2
 4          1              page_under_home2
  5          4              page_under_pageid4_1
   7          5              page_under_pageid5_1
  6          4              page_under_pageid4_2
   9          6              page_under_pageid6_1
   10         6              page_under_pageid6_2
 8          1              page_under_home3
 11         1              page_under_home4
  12         11             page_under_pageid11_1
   13         12             page_under_pageid12_1

我目前有这个:

SELECT     p1.PageId, p1.PageName, p1.PageParent, p2.PageName AS Expr1
FROM         dbo.pages AS p1 FULL OUTER JOIN
                          (SELECT     PageId, PageName
                            FROM          dbo.pages
                            WHERE      (PageParent IS NULL)) AS p2 ON p2.PageId = p1.PageParent

但这几乎不能创建我想要的输出,我想我将完全解决这个问题错误的方式...

编辑:

这就是我目前所拥有的:

WITH 
    PagesMenu(pageId, PageParent, PageName) 
AS 
(
    SELECT     
        PageId, PageParent, PageName
    FROM         
        dbo.pages
    WHERE     
        (PageParent IS NULL) 
        AND 
        (PageIsVisible = 'True')
    UNION ALL

    SELECT     
        b.PageId, b.PageParent, b.PageName
    FROM         
        PagesMenu AS a 
    INNER JOIN
        dbo.pages AS b 
    ON 
        a.pageId = b.PageParent
)

SELECT     pageId, PageParent, PageName
FROM         PagesMenu

它似乎有些工作但不完全重复,第一次递归似乎工作,但看起来第二次不再执行时间。

结果:

pageId    PageParent    PageName
3         NULL          home
1         3             test
4         3             test
5         4             test
6         4             test
7         4             test
8         5             test  <---wrong
2         1             test  <---wrong

I have this table with pages, these pages have parents that are also pages in the same table.

For this examples sake the table looks like:

table: Pages
PageId :Key
PageParent :Foreign Key
PageName

Now my question is what would the SQL look like when creating a menustructure like:

PageId     PageParent     PageName
1          NULL           home
 2          1              page_under_home1
  5          2              page_under_pageid2_1
  6          2              page_under_pageid2_2
 4          1              page_under_home2
  5          4              page_under_pageid4_1
   7          5              page_under_pageid5_1
  6          4              page_under_pageid4_2
   9          6              page_under_pageid6_1
   10         6              page_under_pageid6_2
 8          1              page_under_home3
 11         1              page_under_home4
  12         11             page_under_pageid11_1
   13         12             page_under_pageid12_1

I currently have this:

SELECT     p1.PageId, p1.PageName, p1.PageParent, p2.PageName AS Expr1
FROM         dbo.pages AS p1 FULL OUTER JOIN
                          (SELECT     PageId, PageName
                            FROM          dbo.pages
                            WHERE      (PageParent IS NULL)) AS p2 ON p2.PageId = p1.PageParent

but that doesnt nearly create the output I want and I think I'm going at it completely the wrong way...

EDIT:

this is what I currently have:

WITH 
    PagesMenu(pageId, PageParent, PageName) 
AS 
(
    SELECT     
        PageId, PageParent, PageName
    FROM         
        dbo.pages
    WHERE     
        (PageParent IS NULL) 
        AND 
        (PageIsVisible = 'True')
    UNION ALL

    SELECT     
        b.PageId, b.PageParent, b.PageName
    FROM         
        PagesMenu AS a 
    INNER JOIN
        dbo.pages AS b 
    ON 
        a.pageId = b.PageParent
)

SELECT     pageId, PageParent, PageName
FROM         PagesMenu

And it seems to somewhat work but not completely recurs, the first recursion seems to work but it looks like it doesn't do it a second time.

Result:

pageId    PageParent    PageName
3         NULL          home
1         3             test
4         3             test
5         4             test
6         4             test
7         4             test
8         5             test  <---wrong
2         1             test  <---wrong

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

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

发布评论

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

评论(1

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