How does mysql get the rankings?

suppose the table stu is as follows

id user_id score
1  3        98
2  34       95
3  7        92
4  25       90
The

scores have been sorted in descending order. Now, according to the input user_id (7), the ranking should be 3. How to write the sql statement?

Mar.18,2021

your description of this problem is not clear enough. Have you checked the table you mentioned?
simply write:

in fact, it's just a virtual increment column when querying the table.


aren't user_id (7) and id (3) in the same column?


SELECT IF(
EXISTS(
    SELECT `score`
    FROM `stu`
    WHERE `user_id`=7
),
(
    SELECT COUNT(1)+1
    FROM `stu`
    WHERE `score` > (
        SELECT `score`
        FROM `stu`
        WHERE `user_id`=7
    )
), 
NULL);
Menu