2012-05-01 5 views
0

左の結合が最新のレコードを返さないことを除いて、正しいデータを返すクエリがあります(dateuploaded列による順序付け)。ありがとうございました。mysqlのMAXクエリが結合時に最新のレコードを返さない

select a.*,p.thumbnailphotopath as Picture from album_access ac 
inner join albums a on a.ID = ac.AlbumID 
left join (select albumid, thumbnailphotopath,max(DateUploaded) from photos where IsProcessed = 1 and IsPrivate = 0 GROUP BY albumID) p on a.ID = p.AlbumID #photos record not latest 
where ac.AccessUserID = ? and ac.Ispending = 0 and ac.FullControl = 1 and a.Private = 1 order by a.DateCreated desc limit ?,?; 

List of dates that return desc (its returning 2012-05-01 09:45:43 and thats not the latest) 
2012-05-01 09:46:17 
2012-05-01 09:46:06 
2012-05-01 09:45:43 --Returning this record 
2012-05-01 09:45:30 
2012-05-01 09:39:49 
+0

'...その後マッチングアルバムと日付の修飾子に基づいて正しいアルバムエントリを取得することができ、アルバムごとにアップロードされた最大の日付を取得するprequeryが必要最新のレコードを返す**単一の列**(または式)の最大値を返すことだけが想定されています。 – zerkms

+0

最新の写真のレコードを取得するためにクエリを変更する方法を知っていますか? – user516883

+0

汎用ソリューションは次のようなものです:http://stackoverflow.com/a/6274069/251311 – zerkms

答えて

1

あなたはMAX`が想定されていない

select 
     a.*, 
     pFinal.thumbnailphotopath as Picture 
    from 
     album_access ac 
     inner join albums a 
      on ac.AlbumID = a.ID 
      AND a.Private = 1 
     left join (select 
          p.albumid, 
          max(p.dateUploaded) as MaxDate 
         from 
          photos p 
         where 
           p.IsProcessed = 1 
          AND p.IsPrivate = 0 
         group by 
          p.AlbumID) pMax 
      on ac.AlbumID = pMax.AlbumID 
       left join photos pFinal 
        on pMax.AlbumID = pFinal.AlbumID 
       AND pMax.MaxDate = pFinal.DateUpladed 
    where 
      ac.AccessUserID = ? 
     and ac.Ispending = 0 
     and ac.FullControl = 1 
    order by 
     a.DateCreated desc 
    limit ?,?; 
+0

ありがとうございました。 – user516883

関連する問題