0

我有一个帖子表,其中有一个照片子表,所以一个帖子可以有很多张照片,其中一个照片表列是[优先]。

我需要从 post 表中选择记录,其中只有在另一个表中具有最高优先级的照片:

所以结果应该是这样的:

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

有人可以请帮忙。谢谢。

4

5 回答 5

0

Try with this hint:

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)
于 2013-04-26T04:49:53.100 回答
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,这是为了防止两张照片具有相同的优先级。

编辑我在这里学到了很多关于子查询的知识

于 2013-04-26T05:00:40.387 回答
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
  • 将 ID 过滤到TopPhotos排名最高的 ID
  • 加入原始表以获取您真正想要的记录

显然,如果您需要按需要添加的其他一些标准(如用户)进行过滤。最好在流程中尽早添加(即在 中RankedPhotos),否则数据库最终可能会做比它需要的更多的工作。

于 2013-04-26T04:55:39.383 回答
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
于 2013-04-26T05:22:19.657 回答
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]

我会这样做,内连接和左连接都可以得到你想要的结果。

于 2013-04-26T05:29:37.217 回答