我得到了包含字段的表:id,foreign_key,created,modified
这是一个记录程序部分更改的表。我想要的是检索由外键分组的最新日志。我该怎么做?
编辑: 总结一下,在分组之前我该如何先订购?
答案 0 :(得分:0)
SELECT ... GROUP BY ... HAVING MAX(modified)
答案 1 :(得分:0)
不相关的子查询:
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