在我的代码中,我获得了以下时间的行数:
今天昨天2天前3天前4天前5天前6天前7天前几周本周本月上个月今年
现在每一次都很有效,除了“本月”。似乎没有正确计算。这是我的SQL:
$sql = mysql_query("select `website`,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE()) then 1 else 0 end) AS c_day,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE() - INTERVAL 1 DAY) then 1 else 0 end) AS c_yesterday,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE() - INTERVAL 2 DAY) then 1 else 0 end) AS c_2_days,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE() - INTERVAL 3 DAY) then 1 else 0 end) AS c_3_days,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE() - INTERVAL 4 DAY) then 1 else 0 end) AS c_4_days,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE() - INTERVAL 5 DAY) then 1 else 0 end) AS c_5_days,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE() - INTERVAL 6 DAY) then 1 else 0 end) AS c_6_days,
sum(case when DATE(FROM_UNIXTIME(`date_assigned`)) = DATE(CURDATE() - INTERVAL 7 DAY) then 1 else 0 end) AS c_7_days,
sum(case when WEEK(FROM_UNIXTIME(`date_assigned`)) = WEEK(CURDATE()) then 1 else 0 end) AS c_week,
sum(case when MONTH(FROM_UNIXTIME(`date_assigned`)) = MONTH(CURDATE()) then 1 else 0 end) AS c_month,
sum(case when MONTH(FROM_UNIXTIME(`date_assigned`)) = MONTH(CURDATE() - INTERVAL 1 MONTH) then 1 else 0 end) AS c_last_month,
sum(case when YEAR(FROM_UNIXTIME(`date_assigned`)) = YEAR(CURDATE()) then 1 else 0 end) AS c_year
from `assignments`
where `id_dealership`!='65' and `id_dealership`!='77' and `id_dealership`!='89'
group by `website`
order by `website` asc
") or die(mysql_error());
正如您所看到的,c_month
正是导致我出现问题的原因。考虑到c_last_month工作正常,我不知道为什么会发生这种情况。
再次编辑:
表格结构和一些示例数据:
CREATE TABLE IF NOT EXISTS `assignments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_dealership` int(11) NOT NULL,
`id_lead` int(11) NOT NULL,
`date_assigned` int(11) NOT NULL,
`website` varchar(255) NOT NULL DEFAULT '',
`make` varchar(255) NOT NULL DEFAULT '',
`model` varchar(255) NOT NULL DEFAULT '',
`ip_address` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_dealership` (`id_dealership`),
KEY `id_lead` (`id_lead`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9848 ;
(1165, 40, 1373, 1325540129, 'website.com', 'Ford', 'Edge', ''),
(1164, 34, 1372, 1325539601, 'website.com', 'Ford', 'F-350', ''),
(1163, 34, 1371, 1325537245, 'website.com', 'Ford', 'Focus', ''),
(1162, 34, 1370, 1325529221, 'website.com', 'Ford', 'Flex', ''),
答案 0 :(得分:2)
MySQL MONTH()
从日期开始返回当前月份。这意味着您在脚本中的条件仅匹配:
2012-10-01
- 1天2012-10-05
- 5天2012-10-30
- 30天(正确)因此,您的脚本在最后一天每月运行一次正确:)并且您显示“自本月初以来的观看次数”。
所以你基本上只需要替换条件
sum(case when MONTH(FROM_UNIXTIME(`date_assigned`)) =
MONTH(CURDATE()) then 1 else 0 end) AS c_month
使用这个(可以计算过去31天的项目):
SUM(CASE (`date_assigned` > (CURDATE() - INTERVAL 1 MONTH))
THEN 1 ELSE 0 END) AS c_month
还有一件事,我会担心你的表现。我会在单独的表中计算摘要信息(或将列添加到现有表中)并使用预先计算的数据并使用脚本刷新它们(通过cron执行)。
如果您要求数据准确无误,请在date_assigned
(或更确切地说id_dealership, date_added
)上添加索引,并使用以下内容扩展条件:
AND `date_added` >= (CURDATE() - INTERVAL 1 YEAR))
仅处理一年(而不是更多)的记录。
答案 1 :(得分:0)
你的病情
sum(case when MONTH(FROM_UNIXTIME(`date_assigned`)) = MONTH(CURDATE())
then 1 else 0 end) AS c_month,
不应使用,因为一旦您的数据库包含更多信息,它将不会返回正确的信息。
MONTH('2011-11-14') = MONTH('2012-11-04') = MONTH('2013-11-24')
如果要显示本月的统计信息,则应同时检查MONTH()和YEAR()。本月不仅仅是11月,而是2012年11月。
上个月也一样。