I got the table with the fields: id, foreign_key, created, modified
It's a table that logs the changes of a part of my program. What I want is to retrieve the latest logs grouped by the for开发者_运维百科eign key. How do I do this?
EDIT: to summarize, how do I order first before I group?
SELECT ... GROUP BY ... HAVING MAX(modified)
Uncorrelated subquery:
SELECT t.*
, a.*
FROM mytable t
INNER JOIN
(select b.foreign_key
, max(b.modified) as max_modified
from mytable b
group by 1) a
ON a.foreign_key = t.foreign_key
AND a.max_modified = t.modified
精彩评论