0

I have a system where you can earn points by guessing the good results. When you have the most points, you have to be the number 1. The question is how I can make ranks, so that the guy with the most points will be the number 1, the next guy will be number 2, etc.

Example:

    ID    Points  Rank
  -------------------------
    1   | 1000  | 1
    2   |  900  | 2
    3   |  500  | 5
    4   |  700  | 4
    5   |  800  | 3

Thanks!

Ben
  • 51,770
  • 36
  • 127
  • 149
Jacob
  • 83
  • 8
  • 1
    Welcome to Stack Overflow! How do _you_ think you might do this? How are you looking to do this, in MySQL or PHP? You may find you get a better quality of answer and that people are more willing to help you if you are able to demonstrate that you have [tried something for yourself](http://mattgemmell.com/2008/12/08/what-have-you-tried/). – Ben Jun 17 '12 at 18:58
  • 1
    Possible duplicate of http://stackoverflow.com/questions/3333665/mysql-rank-function – Hobo Jun 17 '12 at 19:00

1 Answers1

0

Let's imagine that you have a database with two tables: the first one lists users and the seccond one lists points earned by questions (the questions come from a third table that's not important although I'll show it below).

User
=====
Id
Name
(...)

Question
=====
Id
(...)

Answer
=====
UserId
QuestionId
PointsTaken

To do a rank from that, you can do a simple SQL statement:

SELECT usr.Id, usr.Name, SUM(ans.PointsTaken)
  FROM User usr
  JOIN Answer ans ON ans.UserId = usr.Id
 GROUP BY usr.Id, usr.Name
 ORDER BY SUM(ans.PointsTaken) DESC

This way, your rank is done. The result will be the person Id, the person Name, and the total of points taken, ordered from max to min. Next to this, you just need to show the information.