有时我们使用mysql的时候想要多个表查询数据,但是关联查询的时候有多条记录,我们只想要最新的一条记录怎么办呢?很多人第一时间想到的是使用group by 处理,这是没问题的,但是需要提取做些处理,要是直接查询出来的数据可能并不准确。
mysql 用 group by 查询时,会自动保留 对应组 ‘最先搜索出来的数据’,但这时数据可能不是最新的
如何设置保留 对应组‘最后搜索出来的数据’ 呢?详见代码
对于mysql 5.5版本
1 2 3 4 5 6 | select * from ( select * from table_name order by create_time desc ) as t group by t.id; |
对于mysql 5.7及以上版本,需要加入limit限制,否则不生效
1 2 3 4 5 6 | select * from ( select * from table_name order by create_time desc limit 100000 ) as t group by t.id; |
转载请保留本文链接:https://www.zhe94.com/878.html
上一篇:vs code 的常用快捷键
下一篇:Kettle 设置和使用变量