Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- How to group by one field and order by another?


LifeinAsia - 4:49 pm on Oct 18, 2012 (gmt 0)


You can do it in a 2-step process:
1) Find the max posted date for each topic-
SELECT topic_id, MAX(posted) AS LastPost
FROM posts
GROUP BY topic_id

2) Match the posts by topic_id and LastPost

The following should work on some systems:
SELECT p.topic_id,p.posted,p.poster,p.message
FROM posts p INNER JOIN (SELECT topic_id, MAX(posted) AS LastPost FROM posts GROUP BY topic_id) m ON (p.topic_id=m.topic_id AND p.posted=m.LastPost)


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4503580.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com