我想获取每个人的最大值的行。使用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个步骤,并且可能有更简单的方法。
答案 0 :(得分:1)
Select * From table t
where views =
(Select Max(views) From table
Where person = t.person)
答案 1 :(得分: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;
答案 2 :(得分:0)
试试这个
select
*
from
data d
where d.views = ( select max(views) from data d2 where d2.person = d.person )