Given my following table structure:
I’m trying to build a query that will return the number of failed login attempts per IP since the last successful login of that same IP within the past hour.
For instance, you’ll notice there are several failed attempts (0) from a single IP within the past hour, but since the last successful login (#145) there has only been 1 (#146), which is what I want returned.
This query should also be dynamic and return rows of grouped IPs.
So far this is what I have, but I think @ipa is returning NULL
SELECT COUNT(*) tries, @ipa := login_ip
FROM login_log
WHERE login_id > (
SELECT MAX(login_id)
FROM login_log
WHERE login_success = 1
AND login_ip = @ipa
)
AND login_success = 0
AND login_date > NOW() - 3600
GROUP BY login_ip
ORDER BY tries DESC;
Thanks
Alias the table and try it this way, as your
@ipavariable is doing nothing for you:Additionally, you can do this with a
join:You can try both ways and see which one is faster for you.