时间:2021-09-06 09:02:40 | 栏目:PHP代码 | 点击:次
这个例子是对课程进度表里面的某个学生的剩余课时进行求和汇总。
laravel 版本是 lts 5.5
StudentLessonProgress::where('student_info_id', $student_info_id) ->sum('total_left_class_num');
打印sql语句如下:
array:1 [? 0 => array:3 [? “query” => “select sum(total_left_class_num) as aggregate from student_lesson_progress where student_info_id = ?” “bindings” => array:1 [▶] “time” => 51.48 ] ]
二、需要多个sum
laravel中怎么实现下面的SQL
select sum(‘profit'),sum(‘order_count') from products where……;
参考
Product::where('status',1)->first( array( \DB::raw('SUM(profit) as profit'), \DB::raw('SUM(order_count) as order_count') ) )->toArray();