数据库: MS SQL 2008
SELECT Listing.Title, Listing.MLS, Pictures.PictureTH, Pictures.Picture, Listing.ID FROM Listing INNER JOIN Pictures ON Listing.ID = Pictures.ListingID WHERE (Pictures.ID = (SELECT MIN(ID) FROM Pictures WHERE (ListingID = Listing.ID)))
问题是,我有几个没有图片的“清单”,由于这个SQL脚本,它们无法显示。如何让他们出现?
如果该值为null,则可能为Pictures.Picture Column提供一个“ default.jpg”值?我对此非常迷失,因此,如果有人可以提供帮助,那就太好了。抱歉,如果我也没有很好地问这个问题,我不明白如何问我真正需要做的事情。但是,请提供更多详细信息,我将其发布。
每个清单可以具有用户想要的图片,即使没有图片,我也需要此脚本来显示清单。
阶段2
谢谢你们。到目前为止,我正在学习一些我什至不知道的新命令。现在的问题是,它为清单中的每张图片返回一行。但是默认图像效果很好。
SELECT Listing.Title, Listing.MLS, coalesce(Pictures.PictureTH, '../default_th.jpg') as PictureTH, coalesce(Pictures.Picture, '../default.jpg') as Picture, Listing.ID FROM Listing LEFT OUTER JOIN Pictures ON Listing.ID = Pictures.ListingID
如何获取它,以便每个ListingID仅返回1行?
两件事情:
left outer join
inner join
coalesce
SELECT Listing.Title , Listing.MLS , Pictures.PictureTH , coalesce(Pictures.Picture, 'default.jpg') as Picture , Listing.ID
FROM Listing LEFT OUTER JOIN Pictures ON Listing.ID = Pictures.ListingID
编辑 限制为一行:
SELECT Listing.Title , Listing.MLS , Pictures.PictureTH , coalesce(Pictures.Picture, 'default.jpg') as Picture , Listing.ID FROM Listing LEFT OUTER JOIN Pictures ON Listing.ID = Pictures.ListingID WHERE Pictures.ID is null OR Pictures.ID = (SELECT MIN(ID) FROM Pictures WHERE (ListingID = Listing.ID)))