A similar approach to this issue by first sorting the table in ascending order of userid
and descending order of id
.
Then grouping the values of the inner
selection by the outer userid
The inner selection
SELECT inner.* FROM table AS inner ORDER BY userid ASC, id DESC;
will give you this intermediate result:
id userid score
-- ------ -----
3 10 70
2 10 89
1 10 55
5 15 80
4 15 50
7 17 99
6 17 90
Grouping this result by the userid
will give you the outcome you want.
SELECT outer.* FROM
(SELECT inner.* FROM table AS inner ORDER BY userid ASC, id DESC) AS outer
GROUP BY outer.userid;
This gives you:
id userid score
-- ------ -----
3 10 70
5 15 80
7 17 99