MySQL相同的最大值,不同的实例

时间:2012-10-17 22:49:02

标签: mysql sql

我有这张桌子:

id | day | count
----------------
 u   tue   1
 u   wed   4
 w   wed   5
 x   mon   5
 y   mon   5
 x   tue   2

我想每天返回计数最多的行。 所以我想得到这张表:

id | day | count
----------------
 w   wed   5
 x   mon   5
 y   mon   5
 x   tue   2

这是我的sql,但它没有给我正确的输出:

select id, day, MAX(count)
from Table
group by day

它给了我:

id | day | count
----------------
 w   wed   5
 y   mon   5
 x   tue   2

2 个答案:

答案 0 :(得分:4)

您可以使用子查询:

select * from table 
  join (select day,max(count) as count from table group by day) as max_rec
    on table.day = max_rec.day and table.count = max_rec.count

sqlFiddle

答案 1 :(得分:2)

select t1.* from
Table t1
JOIN
(
select day, max(count) count
from Table
group by day
)t2 ON t1.day=t2.day AND t1.count=t2.count