Hi guys,
As part of a web application I'm creating, I have a 'timeline' which contains a series of images which I want to retrieve from a database in chronological order. Some of these images are grouped into 'albums', and when this is the case, I only want the query to fetch the first image from that album. To do this, I've been using a GROUP BY clause so that each album only appears once.
Here is my current SQL query:
This almost works. It retrieves the images and groups the albums together so that each album only appears once in the listing.
The only problem is that when the GROUP BY clause consolidates all the images from an album into one record, the image returned is the last image from that album, in chronological order.
Does anyone know how to modify the query so it returns the first result from the group?
(edit) It would probably work if I could incorporate another ORDER BY clause. I could order the images by DESC, then group them. This would ensure that each album was represented by the image I wanted, because the last image returned for each album would actually be the first one. Then I could order again by ASC to put the results back into the right order. But I don't know how to code that query?
As part of a web application I'm creating, I have a 'timeline' which contains a series of images which I want to retrieve from a database in chronological order. Some of these images are grouped into 'albums', and when this is the case, I only want the query to fetch the first image from that album. To do this, I've been using a GROUP BY clause so that each album only appears once.
Here is my current SQL query:
Code:
$sql = "SELECT * FROM Images WHERE timelineID ='" . $image['timelineID'] . "' GROUP BY albumID ORDER BY ImageDate ASC"
The only problem is that when the GROUP BY clause consolidates all the images from an album into one record, the image returned is the last image from that album, in chronological order.
Does anyone know how to modify the query so it returns the first result from the group?
(edit) It would probably work if I could incorporate another ORDER BY clause. I could order the images by DESC, then group them. This would ensure that each album was represented by the image I wanted, because the last image returned for each album would actually be the first one. Then I could order again by ASC to put the results back into the right order. But I don't know how to code that query?
Last edited: