0

I need help with a mysql query to determine a table record ranking based on points. The USERS table has several fields, including user_id, username and point. It can happen that two users have the same score and therefore we have the same level in the ranking. The function I'm going to create will use the user_id to find his ranking

Table USERS
+-----------+------------+----------+
|  user_id  |  username  |  points  |
+-----------+------------+----------+
|     1     |  mike      |        0 |
|     2     |  john      |       55 |
|     3     |  bill      |        1 |
|     4     |  mark      |       20 |
|     5     |  steve     |        0 |
|     6     |  nick      |       55 |
|     7     |  paul      |       13 |
+-----------+------------+----------+

if for example we will look for the user with user_id = 4 we will get 2 as a result (because in the first place we have record with userd_id 2 and 6).

if for example we will look for the user with user_id = 7 we will get 5 as a result (because in the first place we have record with userd_id 2 and 6).

To make the idea better I made a diagram with the rank

+-----------+------------+----------+--------+
|  user_id  |  username  |  points  |  rank  |
+-----------+------------+----------+--------+
|     1     |  mike      |        0 |    5   |
|     2     |  john      |       55 |    1   |
|     3     |  bill      |        1 |    4   |
|     4     |  mark      |       20 |    2   |
|     5     |  steve     |        0 |    5   |
|     6     |  nick      |       55 |    1   |
|     7     |  paul      |       13 |    3   |
+-----------+------------+----------+--------+
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
lizzus
  • 13
  • 4

0 Answers0