Inner join with 3 tables

流过昼夜 提交于 2019-12-06 00:25:01
SELECT  a.*, c.date
FROM    Album a
        INNER JOIN Album_Photo b
            ON a.Album_ID = b.Album_ID
        INNER JOIN Photo c
            ON b.Photo_ID = c.Photo_ID
WHERE   c.Nick = 'owner' AND
        (
          SELECT COUNT(*) 
          FROM   album_photo d
          WHERE  b.album_id = d.album_id AND
                 d.nick = 'owner' AND
                 b.date >= d.date
        ) <= 2   // <<== change this value to 5

I think this can be resolved using Stored Procedures. Using variables and loops, you can retrieve the records you desire. Getting only a maximum of 5 records from an album is quite challenging when you only use the basic SQL commands. Sometimes, you cannot derive the right records. I suggest you use Stored Proc. :)

Try like

"SELECT albums.*,photos.id,photos.path,photo_date as Pht_date
 FROM albums 
     JOIN album_photos 
          ON album_photos.album_id = albums.album_id
     JOIN photos 
          ON photos.photos_id = album_photos_id
"

But you cont give individual limit for photos where it involved in "JOIN" orelse you need to give individually like

$album_ids = "SELECT album_id FROM albums";

then for the photos you need to write query liks

"SELECT photos.* FROM photos 
 WHERE album_photos.album_id in (".$album_ids.") AND album_photos.nick = 'owner'
 JOIN album_photos
 ON album_photos.photo_id = photos.photos_id 
 LIMIT 0,10
 "
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!