我正在尝试创建一个条形图,显示过去 12 个月的每月点击量。
我有以下查询:
SELECT
(MONTH(date)) AS MONTH
, (YEAR(date)) AS YEAR
, SUM(hits) AS count
FROM [statistics]
WHERE ID = '". $id ."'
AND (date > DATEADD(yy, - 1, GETDATE()))
GROUP BY (MONTH(date)), (YEAR(date))
ORDER BY year, month
这提供了以下结果集:
Array
(
[0] => Array
(
[Admin] => Array
(
[month] => 7
[year] => 2012
[count] => 702
)
)
[1] => Array
(
[Admin] => Array
(
[month] => 8
[year] => 2012
[count] => 650
)
)
[2] => Array
(
[Admin] => Array
(
[month] => 9
[year] => 2012
[count] => 670
)
)
[3] => Array
(
[Admin] => Array
(
[month] => 10
[year] => 2012
[count] => 66
)
)
[4] => Array
(
[Admin] => Array
(
[month] => 11
[year] => 2012
[count] => 53
)
)
[5] => Array
(
[Admin] => Array
(
[month] => 12
[year] => 2012
[count] => 39
)
)
[6] => Array
(
[Admin] => Array
(
[month] => 1
[year] => 2013
[count] => 54
)
)
[7] => Array
(
[Admin] => Array
(
[month] => 2
[year] => 2013
[count] => 36
)
)
[8] => Array
(
[Admin] => Array
(
[month] => 3
[year] => 2013
[count] => 48
)
)
)
这仅显示它有结果的月份,但我需要所有月份,无论它们是否有结果(所以计数为 0)。我需要的是更像下面的东西,包括所有 12 个月:
Array
(
[0] => Array
(
[Admin] => Array
(
[month] => 7
[year] => 2012
[count] => 702
)
)
[1] => Array
(
[Admin] => Array
(
[month] => 8
[year] => 2012
[count] => 650
)
)
[2] => Array
(
[Admin] => Array
(
[month] => 9
[year] => 2012
[count] => 670
)
)
[3] => Array
(
[Admin] => Array
(
[month] => 10
[year] => 2012
[count] => 66
)
)
[4] => Array
(
[Admin] => Array
(
[month] => 11
[year] => 2012
[count] => 53
)
)
[5] => Array
(
[Admin] => Array
(
[month] => 12
[year] => 2012
[count] => 39
)
)
[6] => Array
(
[Admin] => Array
(
[month] => 1
[year] => 2013
[count] => 54
)
)
[7] => Array
(
[Admin] => Array
(
[month] => 2
[year] => 2013
[count] => 36
)
)
[8] => Array
(
[Admin] => Array
(
[month] => 3
[year] => 2013
[count] => 48
)
)
[9] => Array
(
[Admin] => Array
(
[month] => 4
[year] => 2013
[count] => 0
)
)
[10] => Array
(
[Admin] => Array
(
[month] => 5
[year] => 2013
[count] => 0
)
)
[11] => Array
(
[Admin] => Array
(
[month] => 6
[year] => 2013
[count] => 0
)
)
)
谁能建议实现这一目标的最佳方法?可以在 SQL 中完成还是需要在代码中完成?如果是这样,怎么做?
提前谢谢了。