获取分组后取某字段最大一条记录
方法一:(效率最高)
select * from test as a
where typeindex = (select max(b.typeindex)
from test as b
where a.type = b.type );
方法二:(效率次之) 有位朋友测试,数据量大的情况下,比第一种方法速度快
select
a.* from test a,
(select type,max(typeindex) typeindex from test group by type) b
where a.type = b.type and a.typeindex = b.typeindex order by a.type
本文介绍了两种从数据库中获取按类型分组后的每组某字段最大值记录的方法。方法一使用子查询来找到每个类型的最大值记录;方法二通过先创建一个包含每组最大值的临时表,然后连接原始表进行匹配。
810

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



