Answer the question
In order to leave comments, you need to log in
How to find out the serial number of a row when sorting?
There are posts, each post has a rating. Is it possible to find out the position of a post in the top, knowing only its rating? Something like, sort by rating and pull out a position in this sort by post id.
Answer the question
In order to leave comments, you need to log in
Count the number of rows whose rating is greater than the given one (if the record with the highest rating is in the top).
SELECT COUNT(*)
FROM `posts`
WHERE `rating` > (
SELECT `rating`
FROM `posts`
WHERE `id` = 42
)
SET @Couner = 0;
SELECT @Counter := @Counter + 1, * FROM table;
And then from the resulting table we select a position by rating.
Find out the position of a post with a rating of 100:
Pull out a post by id:
There may be a problem if there are several posts with a rating of 100...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question