2012-08-29 83 views
0

我知道答案,我的問題有什麼做的排名,只是不知道該怎麼做完全是。我的發言目標是隻獲得5條記錄,但優先考慮某個表格上的記錄而非另一個表格。另一個SQL語句我不明白

我有兩個表:NewsNewsToLocation

讓我們假設有在新聞臺4個新聞和3在滿足我的要求的NewsToLocation。因爲我只能選擇5我要確保在News表中4得到的偏好,然後只從NewsToLocation表中選擇1。我目前的查詢是做相反的,我不知道如何解決它。

這裏是整個查詢。

SELECT 
newSpotlights.* 
FROM 
(SELECT 
    TOP 5 n.news_id, 
    n.is_spotlight, 
    n.location_id, 
    n.news_title, 
    CAST(n.news_content AS varchar(200)) AS news_content, 
    n.writing_date, 
    n.publication_date, 
    n.end_date, 
    n.alt_uri, 
    n.youtube_code, 
    n.icon_img_file, 
    n.banner_img_file 
FROM 
    (SELECT 
     n.news_id 
    FROM rcde_news n 
    WHERE n.location_id = 4 
    AND n.is_spotlight = 1 AND n.publication_date < GETDATE() AND n.end_date > GETDATE() 
    AND n.status_id = 2 /*2 = Active rcde_ItemStatus*/ 
    UNION 
    SELECT 
     n.news_id 
    FROM rcde_newsToLocation ntl 
    INNER JOIN rcde_news n ON ntl.news_id = n.news_id 
    WHERE ntl.location_id = 4 
    AND n.status_id = 2 /*2 = Active rcde_ItemStatus*/ 
    ) AS News 
INNER JOIN rcde_news n ON news.news_id = n.news_id 
WHERE n.is_spotlight = 1 
AND n.publication_date < GETDATE() 
AND n.end_date > GETDATE() 
ORDER BY publication_date DESC 
) AS newSpotlights 

我覺得我應該可以做一些排序最內在的查詢排序,但我不知道該怎麼做。我有點明白排名,但不知道我會怎麼做一個UNION聲明。我可以做一些簡單的事情,比如在聯盟的不同部分分別選擇1作爲等級和2作爲等級,然後按等級排序?

回答

1

您只需添加1秩,2秩到查詢的聯合部分,然後進行排序,最後第一。

SELECT 
    TOP 5 
    n.news_id, 
    n.is_spotlight, 
    n.location_id, 
    n.news_title, 
    CAST(n.news_content AS varchar(200)) AS news_content, 
    n.writing_date, 
    n.publication_date, 
    n.end_date, 
    n.alt_uri, 
    n.youtube_code, 
    n.icon_img_file, 
    n.banner_img_file 
FROM 
    (SELECT 
     1 as rank, 
     n.news_id 
    FROM rcde_news n 
    WHERE n.location_id = 4 
    AND n.is_spotlight = 1 AND n.publication_date < GETDATE() AND n.end_date > GETDATE() 
    AND n.status_id = 2 /*2 = Active rcde_ItemStatus*/ 
    UNION 
    SELECT 
     2 as rank, 
     n.news_id 
    FROM rcde_newsToLocation ntl 
    INNER JOIN rcde_news n ON ntl.news_id = n.news_id 
    WHERE ntl.location_id = 4 
    AND n.status_id = 2 /*2 = Active rcde_ItemStatus*/ 
    ) AS News 
INNER JOIN rcde_news n ON news.news_id = n.news_id 
WHERE n.is_spotlight = 1 
AND n.publication_date < GETDATE() 
AND n.end_date > GETDATE() 
ORDER BY rank DESC, publication_date DESC 
+0

這很簡單。我知道它不必像我一樣努力。 – Leeish

0

是的,你基本上就和您的等級觀念:

select top 5 Column1, Column2 
from (
    select top 5 Column1, Column2, 1 as Rank 
    from News 
    order by SomeColumn 

    union all 

    select top 5 Column1, Column2, 2 as Rank 
    from NewsLocation 
    order by SomeColumn 
    ) x 
order by Rank 
0
SELECT 
    TOP 5 
    n.news_id 
FROM 
    (SELECT 
     1 as ranking, 
     n.news_id 
    FROM rcde_news n 
    WHERE blah blah 
    UNION 
    SELECT 
     2 as ranking, 
     n.news_id 
    FROM rcde_newsToLocation ntl 
    WHERE blah blah 

    ) AS News 
ORDER BY publication_date DESC, Ranking 
+0

我最初嘗試過,但直到我看到下面的一個沒有工作。必須按排名然後排序或排除錯誤。不過謝謝。 – Leeish