问题
I'm trying to implement system-wide login throttling and I need to calculate the daily average number of failed login attempts from the last 3 months.
I'm currently inserting a record on every login fail, each with a timestamp. How can I do this in MySQL?
Thanks in advance for your help
回答1:
SELECT AVG(cnt)
FROM (SELECT COUNT(*) AS cnt
FROM mytable
WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 3 MONTH) AND NOW()
GROUP BY DATE(`date`)) x
Assuming you have a table mytable
with field date
of type date
, datetime
or timestamp
来源:https://stackoverflow.com/questions/5036176/moving-average-mysql