即使计数为 ,结果也应返回带有日期的行null
。
SELECT DATE_FORMAT(created_date, '%Y-%m') AS date, COUNT(*) AS total
FROM mytable
WHERE created_date BETWEEN :startDate AND :endDate
AND condition = :condition
GROUP BY DATE_FORMAT(created_date, '%Y-%m')
ORDER BY created_date ASC
我看到我应该使用LEFT JOIN
and IFNULL
,但我尝试了没有好的结果。
期望的结果:
array(1) {
[0]=> {
["date"]=>
string(7) "2016-01"
["total"]=>
string(1) "0"
}
[1]=> {
["date"]=>
string(7) "2016-02"
["total"]=>
string(4) "9492"
}
[2]=> {
["date"]=>
string(7) "2016-03"
["total"]=>
string(1) "0"
}
}
PS:我正在使用 MySQL