在我的代码中,我得到了以下时间存在的行数:
今天 昨天 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', ''),