MYSQL GROUP BY查询,结果只取最新一条记录

180it 2020-02-18 PM 2416℃ 0条

MYSQL GROUP BY查询,结果只取最新一条记录
mysql 用 group by 查询时,会自动保留 对应组 ‘最先搜索出来的数据’,但这时数据可能不是最新的

如何设置保留 对应组‘最后搜索出来的数据’ 呢?详见代码

对于mysql 5.5版本

select * from (
    select * from table_name order by create_time desc
) as t
group by t.id;
EOT;

对于mysql 5.7版本,需要加入limit限制,否则不生效

select * from (
   select * from table_name order by create_time desc limit 100000
) as t
group by t.id;
EOT;
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

MYSQL GROUP BY查询,结果只取最新一条记录