[英]Getting the latest entries for grouped by the foreign key in MySQL
I got the table with the fields: id, foreign_key, created, modified 我得到了带有以下字段的表:id,foreign_key,已创建,已修改
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 foreign 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.