一对多连接在同一张表的 3 个级别上
我有这个带有页面的表,这些页面的父级也是同一个表中的页面。
对于这个例子来说,表格看起来像:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是关于 CTE 的 MS 链接:
http://msdn.microsoft.com/en-us/library/ms186243。 ASPX
Here is an MS link on CTE's:
http://msdn.microsoft.com/en-us/library/ms186243.aspx