码迷,mamicode.com
首页 > 数据库 > 详细

MySQL之——GROUP BY分组取字段最大值

时间:2017-08-30 15:49:56      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:over   max   inner   group by   分组   row   esc   select   art   

方法一:(效率最高)
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 




方法三:
select a.* from test a inner join (select type , max(typeindex) typeindex from test group by type) b on a.type = b.type and a.typeindex = b.typeindex order by a.type


方法四:(效率最低)
select * from
(
select *,ROW_NUMBER() OVER(PARTITION BY type ORDER BY typeindex DESC) as num
from test
) t
where t.num = 1

MySQL之——GROUP BY分组取字段最大值

标签:over   max   inner   group by   分组   row   esc   select   art   

原文地址:http://www.cnblogs.com/vofill/p/7452752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!