2013-04-26 69 views
0

我有一張照片子表,所以一個職位可以有很多照片,其中一個照片表列是[優先]。SQL Server +選擇與子表項目記錄

,我需要選擇從崗位表中的記錄,只有有另一個表中的最高優先級的照片:

所以結果應該是這樣的:

Photo Post 
pic1 Article1 
picX Article2 

目前我的結果顯示爲

Photo Post 
pic1 Article1 
pic2 Article1 
picX Article2 

與此查詢:

SELECT [Photo], 
     [PostTitle] 
FROM [Post] sp 
INNER JOIN [PostPhotos] spp 
ON (sp.AutoId = spp.PostId) 
WHERE sp.[AutoId] IN (SELECT [PostID] 
         FROM [Favorites] 
         WHERE [UserId] = 'UserXXX') 

我嘗試沒有成功連接查詢:

SELECT photo, 
     [PostTitle], 
     [AskingPrice] 
FROM (SELECT sp.[AutoId], 
       [PostTitle] 
     FROM [SellPost] sp 
     WHERE sp.[AutoId] IN (SELECT [PostID] 
          FROM [Favorites] 
          WHERE [UserId] = 'UserId'))a 
full OUTER JOIN(SELECT TOP 1 [PostId], 
         [photo] 
       FROM [PostPhotos] spp 
       WHERE PostId IN (SELECT [PostID] 
            FROM [Favorites] 
            WHERE [UserId] = 'UserXXX') 
       ORDER BY [Priority] ASC)b 
    on (a.AutoId = b.PostId) 
    order by a.AutoId; 

我的表:

Table Post 
PostId, PostTitle 

Table PostPhotos 
AutoId, PostId, Photo, Priority --> 1 post can have many photos 

有人可以請您幫助。謝謝。

回答

0

對不起,在我的帖子中的錯誤,我想我一直在想太深,我需要的是:

SELECT [p].[AutoId], [PostTitle], [Photo] FROM [Post] p 
     INNER JOIN [PostPhotos] pp 
     ON [p].[AutoId] = [pp].[PostId] 
     WHERE p.[AutoId] IN (SELECT [PostID] FROM [Favorites] WHERE [UserId] = @UserId) 
     AND [Priority] = 1 
0

嘗試用這個提示:

SELECT [p].[PostId],[PostTitle],[Photo] FROM [Post] p 
INNER JOIN [PostPhots] pp 
ON [p].[PostId] = [pp].[PostId] 
WHERE [p].[PostId] 
IN (SELECT TOP 1 [PostId] FROM [PostPhotos] ORDER BY [Priority] DESC) 
0

有幾件事情都不清楚從你的描述(如什麼是AutoId,什麼是PostPhotos主鍵,你有什麼用Favorites做的/那有什麼查表喜歡)。但這裏的總體思路是:

WITH RankedPhotos AS (
    SELECT 
    PostId, 
    AutoId, -- I'm assuming this is the primary key of PostPhotos? 
    RANK() OVER (PARTITION BY AutoId, PostId ORDER BY Priority ASC) AS PhotoRank 
    FROM 
    Post p JOIN 
    PostPhotos pp ON 
     p.PostId = pp.PostId 
), TopPhotos AS (
    SELECT 
    PostId, 
    AutoId 
    FROM 
    RankedPhotos 
    WHERE 
    PhotoRank = 1 
) 
SELECT 
    PostTitle, 
    Photo 
FROM 
    RankedPhotos r JOIN 
    Post p ON 
    r.PostId = p.PostId JOIN 
    PostPhotos pp ON 
    r.AutoId = p.AutoId 

當然你可以解開的CTE和使用嵌套子查詢(你也很可能擺脫TopPhotos完全),但是這可能是更容易看到/明白了。

的基本思路是:

  • RankedPhotos,「等級」的所有Post /PostPhoto
  • 的在TopPhotos過濾器的ID下降到最高等級的那些
  • 加入回原始表格來獲取您實際需要的記錄

顯然,如果您需要按其他條件過濾(如用戶)需要添加。最好儘可能在過程中儘早添加(即在RankedPhotos中),否則數據庫可能最終需要做更多的工作。

0

子查詢不是我的最強點,但我認爲以下可能工作:

SELECT t1.Photo, t2.PostTitle 
FROM Post t2 
    INNER JOIN PostPhotos t1 ON (t2.PostID = t1.PostID) 
WHERE t1.AutoID IN (SELECT TOP 1 d.AutoID, d.PostID 
         FROM PostPhotos As d 
         WHERE d.PostID = t1.PostID 
         ORDER BY d.Priority, d.AutoID) 

注意在子查詢的順序加入d.AutoID的,這是情況下,兩個照片具有相同的優先級。

編輯我學到了很多的東西我知道子查詢here

0
SELECT B.Photo,A.[PostTitle] 
FROM [Post] A INNER JOIN 
     (SELECT AutoId,PostId,Photo,Priority 
     FROM PostPhotos 
     WHERE Priority =1) B ON A.[PostId] = B.[PostId] 

我會這樣做, 和內連接和左連接都可以得到你想要的結果。