假设我有一个用户发送的messages
表,每个表都有一个时间戳。
我想做一个查询,告诉我(历史上)用户在一小时内发送的最多邮件数量。
换句话说,在任何给定的1小时内,发送的消息数量最多。
有什么想法吗?
答案 0 :(得分:2)
需要有关表结构等的更多详细信息,但有类似的内容:
select date(timestmp), hour(timestmp) , count(*)
from yourtable group by date(timestmp) , hour(timestmp)
order by count(*) DESC
limit 100;
会给你想要的结果。
答案 1 :(得分:2)
假设timestamp
为DATETIME - 否则,请使用FROM_UNIXTIME转换为DATETIME ...
对于过去一小时内的[滚动]计数:
SELECT COUNT(*) AS cnt
FROM MESSAGES m
WHERE m.timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 1 HOUR)
AND NOW()
GROUP BY m.user
ORDER BY cnt DESC
LIMIT 1
如果您想要特定小时,请指定小时:
SELECT COUNT(*) AS cnt
FROM MESSAGES m
WHERE m.timestamp BETWEEN '2011-06-06 14:00:00'
AND '2011-06-06 15:00:00'
GROUP BY m.user
ORDER BY cnt DESC
LIMIT 1
答案 2 :(得分:1)
这样的事情应该有效:
SELECT MAX(PerHr) FROM
(SELECT COUNT(*) AS PerHr FROM messages WHERE msg_uid=?
GROUP BY msg_time/3600) t
答案 3 :(得分:1)
我怀疑这会非常缓慢,但是对于一个任意的历史最长时间,这样的事情可能会起作用(如果我离开的话,我就不能投票给我,我不是MySQL人):
SELECT base.user, base.time, COUNT(later.time)
FROM messages base
INNER JOIN messages later ON later.time BETWEEN base.time AND DATE_ADD(base.time, INTERVAL 1 HOUR) AND base.user = later.user
WHERE base.user = --{This query will only work for one user}
GROUP BY base.user, base.time
ORDER BY COUNT(later.time) DESC
LIMIT 1