I have a database table like this:
id    version_id    field1    field2
1     1             texta      text1
1     2             textb      text2
2     1             textc      text3
2     2             textd      text4
2     3             texte      text5
If you didn't work it out, it contains a number of versions of a row, and then some text data.
I want to query it and return the version with the highest number for each id. (so the second and last rows only in the above).
I've tried using group by whilst ordering by version_id DESC - but it seems to order after its grouped, so this doesn't work.
Anyone got any ideas? I can't believe it can't be done!
UPDATE:
Come up with this, which works, but uses a subquery:
SELECT *
FROM (SELECT * FROM table ORDER BY version_id DESC) t1
GROUP BY t1.id
 
     
     
     
     
     
     
     
     
     
    