This is making me crazy! Musing MySQL DBMS (sorry not for specifying that)…
I have following table:
"id", "pingdate", "players"
"3","2012-05-24 11:45:04","63"
"1","2012-05-24 12:15:03","14"
"2","2012-05-24 12:15:03","38"
"3","2012-05-24 12:15:03","24"
"1","2012-05-24 12:45:04","22"
"2","2012-05-24 12:45:04","40"
"3","2012-05-24 12:45:04","66"
"1","2012-05-24 13:15:03","14"
"5","2012-05-24 13:15:03","99"
"3","2012-05-24 13:15:03","63"
"3","2012-05-24 13:15:03","38"
"4","2012-05-24 13:15:03","63"
I want following result:
"5","2012-05-24 13:15:03","99"
"3","2012-05-24 12:45:04","66"
"4","2012-05-24 13:15:03","63"
In text, i want 3 diffent ID´s, with highest players and the correct pingdate.
I have tried this, but it returns the wrong pingdate:
SELECT id,
pingdate,
max(players) AS players
FROM pings
GROUP BY id
ORDER BY players DESC
LIMIT 3
you have to do it with a join or subquery, see sql – getting the id from a row based on a group by