我正在尝试确定 15 分钟内是否发送了超过 5 封具有相同主题和电子邮件的电子邮件。
我目前有这样的查询:
SELECT
frommail,
SUBJECT,
message,
count(*) AS count
FROM
compose
GROUP BY
frommail,
SUBJECT,
message;
但这会返回该表中所有相同记录的计数,但我需要一个条件,即计数在任意 15 分钟间隔内大于 5。
表结构:
CREATE TABLE `compose` ( `id` int(50) NOT NULL AUTO_INCREMENT, `email` varchar(1000) DEFAULT NULL, `cc` varchar(1000) DEFAULT NULL, `bcc` varchar(1000) DEFAULT NULL, `subject` varchar(1000) DEFAULT NULL, `message` varchar(1000) DEFAULT NULL, `files` blob, `frommail` varchar(1000) DEFAULT NULL, `attached` varchar(30) DEFAULT 'no', `vstatus` varchar(30) DEFAULT 'new', `rstatus` varchar(30) DEFAULT 'no', `mstatus` varchar(39) DEFAULT 'inbox', `port` int(10) NOT null, `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
示例数据:
id email cc bc subject message files frommail attached vstatus rstatus mstatus port date 24 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 19:58:20 25 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 19:58:20 26 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 20:23:13 27 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 20:23:13 28 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 20:23:13 29 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 20:23:13 30 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 20:23:13 31 abc Normal mail Hello rohan no new no inbox 7070 2016-02-22 20:34:21 32 abc Normal mail Hello rohan no new no inbox 7075 2016-02-22 20:34:21 33 abc Normal mail Hello rohan no new no inbox 7080 2016-02-22 20:34:21 34 abc Normal mail Hello rohan no new no inbox 8080 2016-02-22 20:34:21 35 abc Normal mail Hello rohan no new no inbox 7070 2016-02-22 20:34:21
我确实有插入时间戳作为日期。
如有任何建议,我们将不胜感激。谢谢。
最佳答案
试试这个伙伴:
Select count(e1.id),
frommail,
SUBJECT,
message
from email e1
left join email e2
on e1.date>date_add(e2.date,interval 15 minute)
having count(*)>5
基本上,它将表与自身进行比较,并显示表中是否有超过 15 分钟的记录与其他记录。
关于mysql - 计算某个时间间隔内选定列中具有相同值的记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35557882/