我有表“图像”,如果存在,我想获取主图像,如果不存在,则获取表中的第一行。我用这个:
SELECT Students.Id, Images.ImagePath
FROM Students INNER JOIN
Images
ON Students.Id = Images.StudentId
AND ( (Images.IsMain = 1)
OR (Images.ImagePath = (
SELECT TOP(1) ImagePath
FROM Images
WHERE StudentId = Students.IdId)))
WHERE Students.Id = @StudentId
如果图像中没有“IsMain”,我只会得到一行,但如果有“IsMain”,我会得到两次。