I have a MySQL statement that pulls back the top 15 scores from a table of users.
select @rownum:=@rownum+1 'rank', driverName, teamColour, totalScore, totalTime, didTyreChange from entries p, (SELECT @rownum:=0) r WHERE totalTime > 1000 and progress = 19 order by totalScore desc, totalTime asc limit 15
The conditions are, that the player must have completed all the questions (where progress = 19) and the final time must be more than 1 second (totalTime > 1000). The statement also returns their rank as a variable (@rownum:=@rownum+1 'rank').
The result of this query is the following
What I now want to do, is extend the results to only show a players top score. I have had a look at DISTINCT
and GROUP BY
but get these results with the following GROUP BY
statement
select @rownum:=@rownum+1 'rank', driverName, teamColour, totalScore, totalTime, didTyreChange from entries p, (SELECT @rownum:=0) r WHERE totalTime > 1000 and progress = 19 group by driverName order by totalScore desc, totalTime asc limit 15
and nowhere with DISTINCT
as I get error code 1064. I have tried both
select @rownum:=@rownum+1 'rank', DISTINCT(driverName), teamColour, totalScore, totalTime, didTyreChange from entries p, (SELECT @rownum:=0) r WHERE totalTime > 1000 and progress = 19 order by totalScore desc, totalTime asc limit 15
and
select DISTINCT driverName, @rownum:=@rownum+1 'rank', teamColour, totalScore, totalTime, didTyreChange from entries p, (SELECT @rownum:=0) r WHERE totalTime > 1000 and progress = 19 order by totalScore desc, totalTime asc limit 15
Neither provides the desired results. I'm wondering if there is an easy way to achieve this within the statement, or to do it on the PHP side of things instead.
The desired resultL
viper 1 9810
Maverick 2 25420
Racer roasty 3 28850
.. .
... 15
In other words, no user should appear in the results twice, only the quickest totalTime is displayed for each user.
Slimmed down SQLFiddle here http://sqlfiddle.com/#!2/36d3dc/7 (8000 char limit reached).