查询以选择自上次成功以来的所有行
鉴于我的以下表结构:
我正在尝试构建一个查询,该查询将返回失败登录尝试次数。
例如,您会注意到过去一小时内来自单个 IP 的多次失败尝试 (0)
,但自上次成功登录 (#145) 以来,只有 1 次 (#146) ,这就是我想要返回的。
此查询也应该是动态的并返回分组 IP 的行。
到目前为止,这就是我所拥有的,但我认为 @ipa
返回 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;
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给表起别名并尝试这种方式,因为您的
@ipa
变量对您没有任何作用:此外,您可以使用
join
来完成此操作:您可以尝试这两种方法并看看哪一个对您来说更快。
Alias the table and try it this way, as your
@ipa
variable 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.