我有以下格式的数据。
iPAddress + timeVisited
=========================
varchar + timestamp
我想要获取的是
- 每天有总 IP 的报告(系统日期)
- 每周有总 IP 的报告(如果今天是星期二,我需要这周而不是持续 7 天)
- 每月有总 IP 的报告(当月......如果今天是 18,那么从本月的 1-18 开始)
- 每年拥有总 IP 的报告
知道如何完成这项工作吗?
获取今天的所有记录
select iPAddress from table_name where date(timeVisited) = curdate();
获取当周的所有记录
select iPAddress from table_name where week(timeVisited) = week(curdate()) and year(timeVisited) = year(curdate());
获取当月的所有记录
select iPAddress from table_name where month(timeVisited) = month(curdate()) and year(timeVisited) = year(curdate());
并获取当年的所有记录
select iPAddress from table_name where year(timeVisited) = year(curdate());
希望能帮助到你。
下面是我用的
select count(*) from myTable where DATE(myTime) = DATE(NOW());
select count(*) from myTable where YEAR(myTime) = YEAR(NOW()) AND WEEKOFYEAR(myTime) = WEEKOFYEAR(NOW());
select count(*) from myTable where YEAR(myTime) = YEAR(NOW()) AND MONTH(myTime) = MONTH(NOW());
select count(*) from myTable where YEAR(myTime) = YEAR(NOW());
YEAR(myTime) = YEAR(NOW())
在本周和当月起着非常重要的作用。
将日期视为2012-10-30
和2013-10-30
。