Answer the question
In order to leave comments, you need to log in
SQL queries: Sample optimization, how are statistics implemented?
There is a user rating table: users_stat(id_user, total - number of rating points). The goal is to determine the place occupied by a certain user in the ranking.
Googling, I found the following solution:
I make two queries to the database:
First, I get the rating of the current user (user id - 4): SELECT total FROM users_stat WHERE id_user = 4;
[It turns out to be equal to, for example, 200 rating points and use it in the next request]
Second, I get this user's place in the rating ( user id - 4):
SET @x:=0;
SELECT num FROM (SELECT @x:[email protected]+1 num, id_user, total FROM users_stat WHERE total > 200-1 ORDER BY total DESC) num WHERE id_user = 4
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question