下面的代码上线后会不会崩 Group by 会不会更好 比他们都好的是(除了单独表已经记录了数值)
// 初始化结果数组 $mOnthlyRecordCounts= []; // 类型 $status = [ '2' => '已上编', '3' => '已撤销', '4' => '已作废' ]; // 循环 12 个月 for ($mOnth= 1; $month <= 12; $month++) { foreach ($status as $key => $value){ // 构造起始和结束日期 $startDate = date('Y-m-01', mktime(0, 0, 0, $month, 1, date('Y'))); $endDate = date('Y-m-t', mktime(0, 0, 0, $month, 1, date('Y'))); // 查询该月的记录数 $count = Db::name('bianzhidan_log') ->where('change_time', '>=', $startDate) ->where('change_time', '<=', $endDate) ->where('status', '=', $key) ->count(); // 将结果添加到数组中 $monthlyRecordCounts[$value][] = $count; } } // 输出结果 print_r($monthlyRecordCounts); 