mysql从今天开始查找日期超过1周

问题描述 投票:0回答:1

我正在尝试查询MySQL中保存的RADIUS数据库,以确定哪些用户尚未登录超过一周。

每个记录都有一个用户名,他们最后一次注册到RADIUS(starttime)以及他们最后一次注册(停止时间)。

starttime和stoptime是时间/日期格式,如'2018-01-16 12:01:00'。

我知道我有一个用户自去年5月以来一直“失败”,所以我用他来测试。

我的第一次测试:

select 
    username,
    TIMESTAMPDIFF( hour, max(stoptime), NOW() )  as diff
from
    radacct
where
    username='waljo'

产生waljo的正确结果,6824

现在,如果我通过添加diff> 168(一周中的小时数)来尝试相同的查询

select 
    username,
    TIMESTAMPDIFF( hour, max(stoptime), NOW() )  as diff
from
    radacct
having diff > 168
and
    username='waljo'

我得到零结果。为什么?

最终,我想做这样的事情,显示任何已断开连接且未启动超过1周的人。

select 
    username,
    TIMESTAMPDIFF( hour, max(stoptime), NOW() )  as diff
from
    radacct
having max(stoptime) > max(starttime)  
and
diff > 168

这也会返回零结果。

mysql sql
1个回答
1
投票

是的,谢谢。

这似乎给出了我想要的结果:

select 
    username,
    TIMESTAMPDIFF( day, max(stoptime), NOW() )  as diff,
    max(stoptime) as maxstop,
    max(starttime) as maxstart
from
    radacct
group by username
having diff > 7
and
maxstop > maxstart
order by diff desc
© www.soinside.com 2019 - 2024. All rights reserved.