我有一个帖子表,其中有一个照片子表,所以一个帖子可以有很多张照片,其中一个照片表列是[优先]。
我需要从 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
有人可以请帮忙。谢谢。