我只想从表产品中选择12个月和总价 年。首先,我声明变量starOfMonth和endOfMonth
$from = Carbon::now()->startOfYear()->format('Y-m-d');
$to = Carbon::now()->endOfYear()->format('Y-m-d');
然后我这样查询
$getTotal = Product::select(
DB::raw('IFNULL(sum(price),0) as sums'),
DB::raw('YEAR(date) year, MONTH(date) month')
)->whereBetween('date', [$from, $to])->groupby('year','month')->get()->toArray();
显示输出如下所示。我已经掌握了正确的数据,但是如果没有数据总和显示为0,如何显示12个月
array:4 [▼
0 => array:3 [▼
"sums" => 90592.0
"year" => 2018
"month" => 6
]
1 => array:3 [▼
"sums" => 95600.0
"year" => 2018
"month" => 9
]
2 => array:3 [▼
"sums" => 12268424.0
"year" => 2018
"month" => 10
]
3 => array:3 [▼
"sums" => 14353968.0
"year" => 2018
"month" => 11
]