1

我想获得每个人的最大值的行。使用 MySQL。

person       page       views
John         home       20
John         cart       15
John         search     43
James        home       32
James        about      41
Kim          cart       5
Kim          contact    3

结果

Person   Page
John     search
James    about
Kim      cart    

一个想法是先得到MAX(views) GROUP BY PERSON然后再做IF(a.views = b.max_views, page, 0),但这似乎需要 3 个步骤,并且可能有一种更简单的方法。

4

3 回答 3

3
 Select * From table t
 where views =
     (Select Max(views) From table
      Where person = t.person)
于 2013-04-09T18:31:50.957 回答
1

您可以使用子查询来获取结果:

select t1.person, t1.page
from yt t1
inner join
(
  select max(views) maxview, person
  from yt
  group by person
) t2
  on t1.person = t2.person
  and t1.views = t2.maxview;

请参阅带有演示的 SQL Fiddle

于 2013-04-09T18:32:11.867 回答
0

试试这个

select 
    * 
from 
   data d
where d.views =  ( select max(views) from data d2 where d2.person = d.person )
于 2013-04-09T18:32:43.593 回答