select * from 表 where id in (select max(id) from 表 group by 去重字段 )
eg:
select * from planthr_info where id in (select max(id) from planthr_info group by name)
本文介绍了一种使用SQL进行去重查询的方法,通过子查询和聚合函数MAX结合IN子句,实现从表中选取每个去重字段下的最新记录。此技巧适用于需要处理大量重复数据的情况,确保每次查询都能获取到每个类别下最新的数据记录。
select * from 表 where id in (select max(id) from 表 group by 去重字段 )
select * from planthr_info where id in (select max(id) from planthr_info group by name)
5458
1251
848

被折叠的 条评论
为什么被折叠?