The result should return a row with the date even if the count is 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
I saw that i should use LEFT JOIN
and IFNULL
, but I tried with no good result.
Desired result :
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 : I'm using MySQL