A
A
Alex Ivanov2015-11-20 19:50:05
MySQL
Alex Ivanov, 2015-11-20 19:50:05

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

2 answer(s)
R
Rsa97, 2015-11-20
@Protossan

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`

Variant with floating window in comment

R
Robot, 2015-11-20
@iam_not_a_robot

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

Well, according to the sql-query with pseudo-code variables, embed them in a string correctly only ))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question