0%

Mysql分组并返回每组里最新的数据

在开发中经常会碰到这个问题,之前自己写的 sql 在测试后发现都是有问题的,下面放上一个比较好的解决方案。

代码如下:

1
2
3
4
5
6
7
8
select t1.*
FROM t_app_seal_user t1
LEFT JOIN t_app_seal_user t2
ON t1.user_uid = t2.user_uid
and t1.seal_type = t2.seal_type
and t1.seal_time < t2.seal_time
where t2.seal_uid is null
order by seal_time desc;

此方法来自: Retrieving the last record in each group

这个方法没有使用 group by ,但是达到了分组的效果,很巧妙。