我有两个表,一个用于存储Album_Name,另一个用于存储Album_Photos
Album_Name
Album_Photos
我想编写一个查询,以便它可以为我提供以下详细信息
Album_Name Album_ID Album_Date ImageSmall Album One 1 2013-08-02 100.jpg Album Two 2 2013-09-09 55.jpg
我想从细节专辑详情Album_Name表,第一图像这我不是分配给从相册Album_Photo表
Album_Photo
我尝试了不适用于的JOINS,然后我创建了一个视图将遵循SQL,这是行不通的
SELECT a.Album_Name AS Album_Name , a.Album_Date AS Album_Date , a.Page_ID AS PageID , p.Image_ID AS Image_ID , p.Image_Small AS Image_Small FROM Album_Name a LEFT OUTER JOIN Album_Photos p ON a.Album_ID = p.Album_ID
我尝试DISTINCT Album_Name过查看它是否使我与上面的语句在同一行
DISTINCT Album_Name
SELECT DISTINCT [Album_Name], Album_Date, Page_ID, Image_Small FROM vw_AlbumName_AlbumPhotos WHERE Page_ID = 3
样本数据Album_Name和Album_Photos表格
Album_ID Album_Name Album_Date Page_ID 1 Album One 2013-08-02 3 2 Album Two 2013-09-09 3 3 Album Three 2013-09-10 9 Image_ID Page_ID Album_ID ImageSmall 1 0 1 100.jpg 2 0 1 21.jpg 3 0 1 36.jpg 4 0 1 44.jpg 5 0 2 55.jpg 6 0 2 66.jpg 7 0 3 10.jpg
任何帮助表示赞赏。
由于每张相册有多张照片,您将获得重复。要获得一个,使用row_number():
row_number()
SELECT Album_Name AS Album_Name, a.Album_Date AS Album_Date, a.Page_ID AS PageID, p.Image_ID AS Image_ID, p.Image_Small AS Image_Small FROM Album_Name a left outer JOIN (select p.*, row_number() over (partition by Album_Id order by Image_ID) as seqnum from Album_Photos p ) p ON a.Album_ID = p.Album_ID and seqnum = 1;