I want to display a yearly report based on month, let say I have orders in January, August & December but for the rest of the year there are no orders.
当我问询使用 CI
select month(order_date) as month_name,
count(order_id) as amount
from order
where year(order_date) = 2011
group by month(order_date)
There is only 3 rows returned.
Say I 然后做:use$query->result_array(
)。
Array ([0] => Array ( [month_num] => 1 [amount] => 4 )
[1] => Array ( [month_num] => 8 [amount] => 1 )
[2] => Array ( [month_num] => 12 [amount] => 19 )
)
This is fine but I need to fill in the rest of the year with the missing months. If the month doesn t exist in the database I need to default the amount to zero.
基本上,如果该月不存在,我将用阿雷拉来最后,用从数据库中得出的数值或零。