A couple months ago I asked a question about something similiar to this, received some help and thought I had the answer. 3 months later I am seeing this didnt work 100% so I need some more help but I can ask the question better now that I understand more.
I have a mysql table with id, userid, rounds, reps, exerciseid
I need to pull a users highest round specific to the exercise I am pulling for. So if the exerciseid was 8 I would need the users top round for that exerciseid but if the user has the same rounds more than once, which happens a lot, then I need to sort that by the reps to give me a true highest performance. Now, after obtaining those results I need to then sort this data set by rounds,reps so if multiple unique users have the same rounds that is then ordered by reps.
Can this be done with pure mysql or am I better off pulling the data and sorting everything with PHP?
SELECT
max(l.rounds) as rounds,
l.reps,l.userid
from leaderboard l
where l.exerciseid = 8
group by l.userid
order by
rounds desc,
reps desc
example of structure
First this is a smaple set
userid exerciseid rounds reps
-- -- --
1 8 23 10
1 8 23 15
1 8 20 10
2 8 28 19
2 8 15 12
3 8 40 29
results I want
userid exerciseid rounds reps
-- -- --
3 8 40 29
2 8 28 19
1 8 23 15
If I understand this correctly, you want to first group by userid and rounds to get the maxreps on a round. Then you want to select the max rounds from this.
Here is one way to express this in MySQL: