MySQL Get row with greatest parent row

If you have a database of CDs, and artists, etc.

Say you want to get a list of artists with their most recent releases:

SELECT 
    artist.name, 
    album.title, 
    album.release_date
FROM artist
LEFT JOIN 
    album ON artist.artistID = album.artistID
WHERE album.albumID = (
    SELECT MAX(albumID) FROM album WHERE artist.artistID =  album.artistID
    )

Make sure you have indexed correctly, this could be a slow one.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>