Answer the question
In order to leave comments, you need to log in
Is it possible to get the number of a specific row when sorting in MySQL
Example: I have N users with exp from 0 to M.
Users are sorted in descending order, from M to 0. Is it possible to get the user number with id in this list?
Now I use REDIS.ZSET for this, but it would be interesting to know the solution on MySQL.
Answer the question
In order to leave comments, you need to log in
--- karma of user n. SET @user_exp = (SELECT exp FROM users where id = {n}); SELECT count(*) FROM users WHERE exp > @user_exp OR (exp = @user_exp AND user.id < {n});
SET @rn := 0;
SELECT * FROM (
SELECT @rn := @rn+1 AS id, exp
FROM users
ORDER BY users DESC
) WHERE id = {n};
something like that
SELECT u1.*, COUNT(u2.id)+1 AS rating FROM users u1, users u2 WHERE u1.id = ? AND u2.exp > u1.exp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question