M
M
Maxim2014-05-24 22:23:53
MySQL
Maxim, 2014-05-24 22:23:53

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

3 answer(s)
A
akashtrih, 2014-05-25
@Frapsy

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
)

S
Stepan, 2014-05-25
@L3n1n

SET @Couner = 0;
SELECT @Counter := @Counter + 1, * FROM table;
And then from the resulting table we select a position by rating.
087e36c77b0b46c49617664a74c3e84b.png6555104899e84a16a073ccf8ebfa439b.png

A
Alexey Kuleshov, 2014-05-25
@GingerbreadMSK

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 question

Ask a Question

731 491 924 answers to any question