选择列中包含max的行 - MySQL

时间:2012-10-25 07:58:30

标签: mysql sql

我有下表名为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的行。所以这将是一个导致

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

3 个答案:

答案 0 :(得分:5)

试试这个:

select * 
from   MyTable T
join      (Select First,max(Fourth) as Fourth
          from MyTable
          group by First)a
on    T.First=a.First
and   T.Fourth=a.Fourth


SQL Fiddle Demo

答案 1 :(得分:2)

试试这个解决方案:

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

SQL Fiddle演示

答案 2 :(得分:0)

select
    B.*
from (select T.First, max(T.Fourth) as Fourth from Table as T) as A
    inner join Table as B on B.First = B.First and B.Fourth = A.Fourth