Undefined Variable
Undefined Variable

Reputation: 4267

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...

Upvotes: 1

Views: 58

Answers (1)

smurfAccount
smurfAccount

Reputation: 33

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

Upvotes: 2

Related Questions