基于多联合中的一列的不同行

时间:2013-01-03 05:26:07

标签: sql sql-server sql-server-2008 distinct union

我正在建立一个基于4表的分层菜单。在做研究之后,我能想到的唯一解决方案就是到目前为止我已经接近联盟,但我仍然没有达到预期的效果。

4我正在使用的表是

  1. PageID,其中PageID为PK (其中包含cms的实际页面)
  2. 文章表,其中PageID为FK (有文章)
  3. 文章类别表,其中PageID为FK (此表有文章类别)
  4. 杂志表,其中PageID为FK (杂志表保存杂志信息)
  5. 我必须根据对previous question which is directly related to this question的一些建议对我的数据库进行一些更改,之前的问题包含有关数据库的详细信息。

    我需要这些列PID, MENU, Handler,PageLangID, ParentID,IssueID, CatID,MenuPosition 创建我的分层菜单我写了下面的内容,它为我提供了所需的列,但我无法在菜单上运行DISTINCT,因此我只能获得Unique Row,并根据该结果集我可以创建我的菜单。

    当我尝试添加DISTINCT (Menu)时,它会生成语法错误。

    根据我在previous question提到的要求,我不确定这种方法是否合适,或者我应采取其他方法,这种方法不含糊或更专业

    QUERY

     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
    

2 个答案:

答案 0 :(得分:0)

您在外部select语句中使用了order by。 由于没有使用distinct语句的顺序,

因此,在子选择中执行DISTINCT,在外部选择中执行ORDER BY。

希望这有助于解决您的问题。

答案 1 :(得分:0)

您需要在所有SELECT语句上使用DISTINCT

SELECT DISTINCT PID, MENU, Handler, PageLangID, ParentID,IssueID, CatID, MenuPosition
...
相关问题