业务场景:
在针对某个类型数据存在多条时,但只想取最新的一条。在可以确定时哪种类型时我们使用简单sql就可以解决。
如: select * from t_table a where a.type=? order by time desc limit1;
但是在我们不确定时哪种类型时,需要全表扫描或者多数据扫描时,就需要用到排序+分组的方式解决了。
如:select a.type,max(id) from( select * from t_table order by time desc ) a group by a.type;