我正在努力构建一个基于 4 个表的分层菜单。在进行研究之后,我能想到的唯一解决方案是与 UNION 一起做,到目前为止我很接近,但我仍然没有得到想要的结果。
4 我正在使用的表是
- 以 PageID 为 PK 的页表(其中包含 cms 的实际页面)
- PageID 为 FK 的文章表(有文章)
- PageID 为 FK 的文章分类表(该表有文章分类)
- 以 PageID 为 FK 的杂志表(杂志表保存杂志信息)
我必须根据对与此问题直接相关的上一个问题提出的一些建议对我的数据库进行一些更改,上一个问题包含有关数据库的详细信息。
我需要这些列PID, MENU, Handler,PageLangID, ParentID,IssueID, CatID,MenuPosition
来创建我的分层菜单我在下面写了我需要的列,但我无法在菜单上运行 DISTINCT,因此我将只获得唯一行并基于该结果集我可以创建我的菜单。
当我尝试添加DISTINCT (Menu)
它时会产生语法错误。
根据我在上一个问题中提到的要求,我不确定这种方法是否可行,或者我应该采取其他一些不模棱两可或更专业的方法
询问
SELECT PID, MENU, Handler, PageLangID, ParentID,IssueID, CatID, MenuPosition
FROM (
--Pages Table
SELECT PageId AS PID,SUBSTRING(PageName,0,20) AS MENU,SUBSTRING(PageInternalLinkURL,0,24) AS Handler, PageLangID,PageInheritance AS ParentID, 1 AS IssueID, 1 AS CatID,
PageLinkPosition as MenuPosition FROM pg_Pages WHERE PageLangID = 1
UNION
--Article Table
SELECT p.PageID as PID, SUBSTRING(c.ArticleCategoryName,0,20) AS MENU,SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler,LanguageID,p.PageID AS ParentID,IssueID,c.ArticleCategoryID AS CatID,
1 AS MenuPosition FROM art_Articles a JOIN art_Category c ON a.ArticleCategoryID = c.ArticleCategoryID JOIN pg_pages p ON p.PageID = a.PageID WHERE LanguageID =1
UNION
--Article Category Table
SELECT p.PageID AS PID, SUBSTRING(c.ArticleCategoryName,0,20) AS MENU,SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler, LangID,p.PageID,1, ArticleCategoryID,
1 AS MenuPosition FROM art_Category c JOIN pg_Pages p ON c.PageID = p.PageID WHERE LangID =1
UNION
--Magazine Table
SELECT p.PageID AS PID, CAST(IssueCode AS varchar(10)),SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler,LangID,p.PageID AS ParentID, m.IssueID AS IssueID, 1,
1 AS MenuPosition FROM Magazine m JOIN pg_pages p ON m.PageID = p.PageID WHERE LangID =1
) AS T WHERE T.PageLangID = 1 ORDER BY MenuPosition
输出
PID MENU Handler PageLangID ParentID IssueID CatID MenuPosition
----------- -------------------- ------------------------ ----------- ----------- ----------- ----------- ------------
5 Book Review Articles.aspx 1 5 5 18 1
5 Business Articles.aspx 1 5 5 16 1
5 Group News Articles.aspx 1 5 5 6 1
5 Infrastructure Articles.aspx 1 5 5 17 1
5 Politics Articles.aspx 1 5 1 1 1
5 Politics Articles.aspx 1 5 3 1 1
5 Politics Articles.aspx 1 5 4 1 1
5 Politics Articles.aspx 1 5 5 1 1
6 Book Review Article-Category.aspx 1 6 1 18 1
6 Business Article-Category.aspx 1 6 1 16 1
6 Chairman's Message Article-Category.aspx 1 6 1 9 1
6 Culture Article-Category.aspx 1 6 1 3 1
6 Economy Article-Category.aspx 1 6 1 2 1
6 Editorial Message Article-Category.aspx 1 6 1 8 1
6 Finance Article-Category.aspx 1 6 1 19 1
6 Group News Article-Category.aspx 1 6 1 6 1
6 Habtoor Leighton Gr Article-Category.aspx 1 6 1 5 1
6 Infrastructure Article-Category.aspx 1 6 1 17 1
6 Lifestyle Article-Category.aspx 1 6 1 20 1
6 People Article-Category.aspx 1 6 1 7 1
6 Politics Article-Category.aspx 1 6 1 1 1
6 Sports Article-Category.aspx 1 6 1 4 1
12 102 Default.aspx 1 12 3 1 1
12 103 Default.aspx 1 12 4 1 1
12 106 Default.aspx 1 12 1 1 1
12 109 Default.aspx 1 12 5 1 1
1 Home Default.aspx 1 0 1 1 10
11 Video Videos.aspx 1 10 1 1 10
2 About Us Page.aspx 1 0 1 1 20
5 Articles Articles.aspx 1 0 1 1 20
6 Categories Article-Category.aspx 1 0 1 1 25
3 News News.aspx 1 0 1 1 30
12 Archive Default.aspx 1 0 1 1 40
10 Multimedia Multimedia.aspx 1 0 1 1 60