Answer the question
In order to leave comments, you need to log in
How to choose with time comparison within a limited range?
Help advice. There is a table of posts on the site, and you need to find 10 participants who left the most messages in 60 minutes while maintaining the id of these posts.
Those. someone can post 100 messages a day, every 15-20 minutes, and someone posts only 50 but for 60 minutes. Here I need to find 10 of those who left the most posts per hour from all who are in the table.
The "posts" table has fields
usrid | data | post | postid
Truth is given that one request here will not get off in any way. Or is it real?
Answer the question
In order to leave comments, you need to log in
If for a specific time interval, then
SELECT `t1`.`usrid`, `t2`.`data`, `t2`.`postid`
FROM (
SELECT `usrid`, COUNT(*) AS `cnt`
FROM `posts`
WHERE `data` BETWEEN :start AND :end
GROUP BY `usrid`
ORDER BY `cnt` DESC
LIMIT 10
) AS `t1`
JOIN `posts` AS `t2` USING(`usrid`)
WHERE `t2`.`data` BETWEEN :start AND :end
ORDER BY `t1`.`cnt`, `t1`.`usrid`, `t2`.`data`
It depends on what form the time is in, if datetime or timestamp then:
// Начало часа
$N1 = date("2015-07-26 14:00");
// Конец часа
$N2 = date("2015-07-26 15:00");
//Теперь простой запрос с BEETWEN
SELECT `column_name`
FROM `table_name`
WHERE `time` BETWEEN $N1 AND $N2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question