鉴于此表:
CREATE TABLE `items` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`type` char(15) DEFAULT NULL,
`ext_id` varchar(255) DEFAULT NULL,
`timestamp` int(11) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`txt` text,
`url` varchar(255) DEFAULT NULL,
`longitude` float(9,6) DEFAULT NULL,
`latitude` float(9,6) DEFAULT NULL,
`meta` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41695 DEFAULT CHARSET=utf8;
数据范围从 2005 年到 2013 年,所有数据都在 (int) 时间戳列中,此查询:
SELECT DATE_FORMAT(`timestamp`, '%Y') as 'year',
COUNT(id) as 'total'
FROM items
GROUP BY DATE_FORMAT(`timestamp`, '%Y')
返回这个:
'year' => null
'COUNT(*)' => string '38710' (length=5)
'year' => string '2011' (length=4)
'COUNT(*)' => string '45' (length=2)
'year' => string '2012' (length=4)
'COUNT(*)' => string '67' (length=2)
'year' => string '2013' (length=4)
'COUNT(*)' => string '90' (length=2)
为什么我没有得到正确的结果?当我遍历 aSELECT * FROM items
时,我在打印时得到了正确的日期<?php print date('Y-m-d H:i', $item->timestamp); ?>
,所以日期就在那里。