I have a users
table that has a column called money_sent
. I want to order this table by money_sent
in descending order, and then find out what "rank" a specific user has.
For example, only 111 people have spent more money than User 12392, so they would be rank 112.
How could I query this?
MySQL doesn't support ROWNUM() function, but it since version 8.0, MySQL introduced ROW_NUMBER() function as an equivalent to return the number of the current row within its partition during data retrieval.
Notice that MySQL has supported the ROW_NUMBER() since version 8.0. If you use MySQL 8.0 or later, check it out ROW_NUMBER() function. Otherwise, you can continue with the tutorial to learn how to emulate the ROW_NUMBER() function.
How about:
SELECT count(*) FROM users WHERE money_sent < (
SELECT money_sent FROM users WHERE user = 'joe'
);
SELECT Row,user, money_sent
FROM (SELECT @row := @row + 1 AS Row, user, money_sent
FROM table1 order by money_sent desc)
As derived1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With