选择列中具有最大值的行-MySQL [英] Selecting the row with the max in a column - MySQL

查看:96
本文介绍了选择列中具有最大值的行-MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyTable的表

I have the following table called MyTable

    First     Second     Third    Fourth
    1         Ab         Cd       2.3
    1         Cq         Fe       3.4
    2         Dr         Dh       1.2
    3         Bb         Qd       9.8
   ..........ETC.....................

如何选择按First分组的具有最大Fourth列值的行.因此,这将是导致查询的查询

How can I select the rows grouped by First that have a maximum Fourth column value. So it would be a query that results in

    First     Second     Third    Fourth
    1         Cq         Fe       3.4
    2         Dr         Dh       1.2
    3         Bb         Qd       9.8

推荐答案

尝试以下解决方案:

select MT.First, MT.Second, MT.Third, MT.Fourth 
from MyTable MT
join ( select first, max(Fourth) as Fourth
       from MyTable
       group by first
     ) T on T.first = MT.First
         and T.Fourth = MT.Fourth

这篇关于选择列中具有最大值的行-MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆