我希望监控网站上不同类型的操作,我将使用登录作为示例。
user_logins table:
user_id name when_logged
------- ---- -----------
smallint varchar(128) timestamp
因此,要查看我今天登录的每个用户(以及使用了多少次):
select name, count(name) from user_logins
where when_logged between '2012-11-17 00:00:00' AND '2012-11-17 23:59:59'
group by name;
但是,我希望在过去的14天里,每天分开......
是否可以在一个查询中执行此操作,而不是通过更改日期来执行14个查询?
答案 0 :(得分:1)
select name, count(when_logged)
from user_logins
where when_logged >= curdate() - interval 14 day
group by name, date(when_logged)
答案 1 :(得分:1)
select date(when_logged) as datelogged, name, count(name)
from user_logins
where when_logged >= date_sub(current_timestamp, INTERVAL 14 day)
group by name, date(when_logged);