Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL 5.7 Getting the row number

Tags:

mysql

I have a database called "playerrank" that has points column. I want to show on people's profile page their rank like this:

Rank: 3/1456

I tried using ROW_NUMBER() but it seems like my host has low version (5.7 i believe).its giving me errors.

Is there another way i can get the ranking of a player based on points other than ordering the db by points desc and getting the row number somehow?

like image 715
Prx Avatar asked Feb 03 '26 07:02

Prx


1 Answers

In MySQL 5.7, in a single query

SELECT 
    (@row_number := @row_number + 1) AS rnk, points
FROM yourTable,
(SELECT @row_number := 0) AS x
ORDER BY points DESC;
like image 162
aRvi Avatar answered Feb 05 '26 22:02

aRvi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!