2个表
相册[Fct_Album]
[AlbumId]
[AlbumName]
[UserId]
[Disabled]
[CreateTime]
[CreateBy]
[ModifyTime]
[ModifyBy]相片[Rel_AlbumPhoto]
[AlbumPhotoId]
[AlbumId]
[PhotoTitle]
[PhotoURL]
[PhotoRe]
[Hit]
[FrontCover]
[Disabled]
[CreateTime]
[CreateBy]
[ModifyTime]
[ModifyBy]关系当然是一对多,现在要查出相册并且用相册中一张图片作为封面。[FrontCover]就是设定的封面int型1就是封面,但不是所有相册都设有封面没有封面的要用最新的图片[CreateTime]作为封面,相册里没有图片不显示。
相册[Fct_Album]
[AlbumId]
[AlbumName]
[UserId]
[Disabled]
[CreateTime]
[CreateBy]
[ModifyTime]
[ModifyBy]相片[Rel_AlbumPhoto]
[AlbumPhotoId]
[AlbumId]
[PhotoTitle]
[PhotoURL]
[PhotoRe]
[Hit]
[FrontCover]
[Disabled]
[CreateTime]
[CreateBy]
[ModifyTime]
[ModifyBy]关系当然是一对多,现在要查出相册并且用相册中一张图片作为封面。[FrontCover]就是设定的封面int型1就是封面,但不是所有相册都设有封面没有封面的要用最新的图片[CreateTime]作为封面,相册里没有图片不显示。
*
FROM [Fct_Album] AS a
INNER JOIN [Rel_AlbumPhoto] AS b ON a.[AlbumId]=b.[AlbumId]
WHERE [AlbumPhotoId]=(SELECT TOP 1 [AlbumPhotoId] FROM [Rel_AlbumPhoto] WHERE [AlbumId]=b.[AlbumId] ORDER BY CASE WHEN [FrontCover]=1 THEN [FrontCover] ELSE 2 END ASC,[CreateTime] desc)
AlbumPhotoId AlbumId PhotoURL CreateTime FrontCover
1 1 1.jpg XXXXXXXXX 1
2 1 2.jpg XXXXXXXXX 0
3 2 3.jpg XXXXXXXXX 0
4 2 4.jpg XXXXXXXXX 0
5 3 5.jpg XXXXXXXXX 0相册
AlbumId CreateTime .....
1 xxxxxxxx
2 xxxxxxxx
3 xxxxxxxx最后结果
AlbumId CreateTime AlbumPhotoId PhotoURL
1 xxxxxxxx 1 1.jpg
2 xxxxxxxx 3 3.jpg
3 xxxxxxxx 5 5.jpg
b.AlbumId,a.CreateTime,a.AlbumPhotoId,a.PhotoURL
from
相片 a,相册 b
where
a.AlbumId=b.AlbumId
and
a.AlbumPhotoId=(select max(AlbumPhotoId) from 相片 where AlbumId=a.AlbumId)