I have a table like this
+-----+-------+
| pid | score |
+-----+-------+
| 1 | 120|
| 2 | 130|
| 3 | 100|
| 1 | 120|
| 2 | 130|
| 3 | 100|
+-----+-------+
I am trying to get get my users ranking. Currently I am using this query,
SELECT pid
, SUM(score) AS total_score
FROM score
GROUP
BY pid
ORDER
BY total_score DESC
I am getting a sorted list by score with this query but not getting their position. I want something like this
+-----+-------+------+
| pid | score | rank |
+-----+-------+------+
| 2 | 260| 1 |
| 1 | 240| 2 |
| 3 | 200| 3 |
+-----+-------+------+
And can I get this position for a specific user? Like
+-----+-------+------+
| pid | score | rank |
+-----+-------+------+
| 1 | 240| 2 |
+-----+-------+------+
I am using MySQL 5.7.21. Can you please help?
SELECT * FROM (SELECT ...) WHERE pid = 2
– mvp Jun 15 '18 at 16:26