简体   繁体   中英

MySQL: fetching only one row per person which has highest value

I have a table that stores top three high scores of each player. Example data is:

id     player_id     score        score_date
------------------------------------------------------
1      100           1100         2015-10-10
2      101           1750         2015-10-07
3      100           3200         2015-10-10
4      102           4100         2015-10-10
5      101           3000         2015-10-01

How can I write a query that will fetch me the highest score record of the each player for a particular date? Say my date is 2015-10-10 , the result I want is:

player_id     score        date
------------------------------------------------------
100           3200         2015-10-10
102           4100         2015-10-10

If you notice, the player 100 has two scores on the said date. I tried:

select * from player
where  date(score_date) = date('2015-10-10')
group by player_id

But it gave me 1100 as score for player 100 , but correct value is 3200 . I also tried adding ORDER by score DESC limit 1 to end of the above query but then the results were just weird...

I hope the MySQL gurus in SO will take mercy on me and help...

select player_id, MAX(score), score_date from player
where  date(score_date) = date('2015-10-10')
group by player_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM