开发者

Getting the latest entries for grouped by the foreign key in MySQL

开发者 https://www.devze.com 2022-12-16 09:38 出处:网络
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开发者_运

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
0

精彩评论

暂无评论...
验证码 换一张
取 消