Answer the question
In order to leave comments, you need to log in
How to sort and select only popular posts?
Tell me how to implement the selection and sorting of only popular records
Table:
id|timestamp|rating
How to implement the following:
Sort for the beginning by time, then re-sort what happened after the first sorting but by rating (during the day) and so that in the final selection for one day there were no more than 10 records
Answer the question
In order to leave comments, you need to log in
SELECT
`id`,
`timestamp`,
`rating`
FROM
(
SELECT
`id`,
`timestamp`,
`rating`,
@timestamp_rank := IF(@current_timestamp = `timestamp`, @timestamp_rank + 1, 1) AS `timestamp_rank`,
@current_timestamp := `timestamp`
FROM
`rating_log`
ORDER BY
`timestamp` DESC,
`rating` DESC
) `ranked`
WHERE
`timestamp_rank` <= 10;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question