[thelist] mysql - group by & order by

Joe Flintham list at menticulture.com
Sun Dec 7 03:05:08 CST 2008

Hi there

I have a single table, of the form

s_id     s_group     s_value     s_timeStamp
1          100            a               1228120256
2          100            b               1228120257
3          101            c               1228120258
4          102            d               1228120259
5          102            e               1228120260

I want to build a query which selects only the most recent item from 
each s_group

I've tried the very simple:

SELECT s_id, s_group, s_value, s_timeStamp FROM tbl
GROUP BY s_group
ORDER BY s_timeStamp DESC

unfortunately, this applies the GROUP BY before the ORDER BY, so returns 
the first item from each group (i.e. the one with the lowest s_id).

Any suggestions, assuming I've explained this sufficiently?



More information about the thelist mailing list