zoukankan      html  css  js  c++  java
  • MySQL之——GROUP BY分组取字段最大值

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

  • 相关阅读:
    [ZJOI2010]数字计数
    [SCOI2009]windy数
    [Tjoi2018]数学计算
    [ZJOI2008] 骑士
    [CQOI2009] 中位数
    11.7 模拟赛
    10.31 模拟赛
    随机游走
    10.29 模拟赛
    10.28 模拟赛
  • 原文地址:https://www.cnblogs.com/vofill/p/7452752.html
Copyright © 2011-2022 走看看