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

时间:2018-02-16 21:31:47

标签: mysql sql

我正在尝试查询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

这也会返回零结果。

1 个答案:

答案 0 :(得分: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