如果在最近15分钟内使用相同内容发送了超过3次的字段cmd,是否有可以匹配的查询?
我目前有这个:
SELECT COUNT(*)
FROM `log`
WHERE gid = @gid
AND ts_register BETWEEN DATE_SUB(NOW() , INTERVAL 15 MINUTE) AND NOW()
这将收集全局ID在过去15分钟内发送的所有邮件。
有没有办法考虑字段cmd并列出gid是否在过去15分钟内发送了多次重复cmd 3次?
例如:
gid 1 cmd testing repeatable cmd
gid 1 cmd testing repeatable cmd
gid 1 cmd testing repeatable cmd
gid 1 cmd different cmd
所以它会给我3分。
答案 0 :(得分:4)
你想这样做:
SELECT cmd, COUNT(*) Qty
FROM `log`
WHERE gid = @gid
AND ts_register BETWEEN DATE_SUB(NOW() , INTERVAL 15 MINUTE) AND NOW()
GROUP BY cmd
鉴于您的数据,将返回:
Cmd Qty --- --- gid 1 cmd testing repeatable cmd 3 gid 1 cmd different cmd 1
<强>更新强>
使用@Gidon的建议(Upvoted you,man!),您可以添加HAVING
子句以仅显示超过3的子句:
SELECT cmd, COUNT(*) Qty
FROM `log`
WHERE gid = @gid
AND ts_register BETWEEN DATE_SUB(NOW() , INTERVAL 15 MINUTE) AND NOW()
GROUP BY cmd
HAVING COUNT(*) > 3
答案 1 :(得分:2)
将此添加到Adrian的查询中以获取超过3个条件。
HAVING COUNT(*) > 3