This is table album
and it's structure and data is,
This is another table photos
and it's structure and data is,
I would like to list all the album name, total photos for that corresponding album, last uploaded image by JOIN and GROUP BY.
This is the Codeigniter query I'm using right now. Everything works except it fetches the first row of image
column value instead of latest one.
$query = $this->db->select('albums.name, photos.image, albums.created, albums.id,
COUNT(photos.id) as total')
->from('photos')
->join('albums', 'albums.id = photos.album_id')
->order_by('photos.id', 'desc')
->group_by('photos.album_id')
->limit($limit, $offset)
->get();
The generated SQL query for above is,
SELECT `albums`.`name`, `photos`.`image`, `albums`.`created`, `albums`.`id`, COUNT(photos.id) as total FROM `photos` JOIN `albums` ON `albums`.`id` = `photos`.`album_id` GROUP BY `photos`.`album_id` ORDER BY `photos`.`id` DESC LIMIT 10
For both the query the result is,
[{"name":"Movie Stills","image":"assets/images/albums/2016/01/84786b91857b45fa9391943e9a468ac6.jpg","created":"2016-01-19 23:41:53","id":"3","total":"3"},{"name":"Firstlook","image":"assets/images/albums/2016/01/6e90cdfdec444dfc53f4a7c30a2ac31e.jpg","created":"2016-01-19 23:16:05","id":"1","total":"2"}]
If you look the image key, it shows the value of first one instead of last row.
I'm open to any of both query solution (Codeigniter/SQL).