Answer the question
In order to leave comments, you need to log in
How to find out the user's place depending on the points scored?
Hello!
Imagine a table with the following structure and one million rows.
CREATE TABLE `table` (
`user_id` int(11) NOT NULL DEFAULT 0,
`points` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
Answer the question
In order to leave comments, you need to log in
SELECT COUNT(*) + 1
FROM `table`
WHERE `points` > (
SELECT `points`
FROM `table`
WHERE `user_id` = :userId
LIMIT 1
)
To speed up it is worth creating an index on the points field. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question