我有一个查询,左边的连接部分应该返回数据或null。但即使有现有记录,它也不会返回任何数据。左连接部分的扭曲是我想只检索一条记录(如果存在)。谢谢你的帮助。
select a.*,p.thumbnailphotopath as AlbumPicture
from (select * from album_access) ac
inner join (select * from albums) a on a.ID = ac.AlbumID
left join (
select * from photos
where IsProcessed = 1 order by DateUploaded desc limit 1
) p
on a.ID = p.AlbumID #should return one if exist.
where ac.AccessUserID = '35e44a8e-643a-4c4f-8a46-59911a1e7c53'
and ac.FullControl = 1 and a.Private = 1
答案 0 :(得分:2)
首先,您可以加入表名,而不需要加入整个SELECT * FROM语句。
其次,您应该尝试不使用SELECT *而是选择所需的列。
但我认为您的LEFT JOIN的问题在于您加入的子查询只返回一个结果,这将是上次上传的照片中的条目,而不管它属于哪个相册ID。如果您希望每行上一次上传日期的照片中的条目,请尝试使用此类
SELECT a.*,p.thumbnailphotopath AS AlbumPicture
FROM album_access ac
INNER JOIN albums a ON a.ID = ac.AlbumID
LEFT JOIN (
SELECT albumID,MAX(DateUploaded) FROM photos
WHERE IsProcessed = 1 GROUP BY albumID
) p ON a.ID = p.AlbumID #should return one if exist.
WHERE ac.AccessUserID = '35e44a8e-643a-4c4f-8a46-59911a1e7c53'
AND ac.FullControl = 1
AND a.Private = 1