A
A
Alex Ponomarev2020-06-03 18:40:37
PHP
Alex Ponomarev, 2020-06-03 18:40:37

How to determine where in the ranking a user is?

There is a list of users, each has a percentage of progress.
I need the user to see where he is among all the students.
I've been trying to find similar information for a long time but I can't.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Tsvetkov, 2020-06-03
@rusindex

window function. ROW_NUMBER . Or RANK . The implementation is determined by the specific DBMS.

G
galaxy, 2020-06-03
@galaxy

What DB? Mysql has variables and you can do something like this:

SELECT    name,
          percent,
          @curRank := @curRank + 1 AS rank
FROM      students s, (SELECT @curRank := 0) r
ORDER BY  percent desc;

(I can't vouch for the syntax)
The normal way to do this, and now more or less portable, is via window functions:
SELECT name,
       percent,
       dense_rank() over (order by percent desc) rating
  FROM students
 ORDER BY 1;

F
FanatPHP, 2020-06-03
@FanatPHP

Lord, people have forgotten how to write SQL
just a little - variables, window functions. write the procedure

SELECT count(*)+1 FROM players WHERE score > (SELECT score FROM players WHERE id = ?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question