How to add ranks to a point system
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.
ID Points Rank ------------------------- 1 | 1000 | 1 2 | 900 | 2 3 | 500 | 5 4 | 700 | 4 5 | 800 | 3
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.