Laravel 对某一列进行筛选然后求和 sum()

时间:2022-03-25 19:20:16

这个例子是对课程进度表里面的某个学生的剩余课时进行求和汇总。
laravel 版本是 lts 5.5

StudentLessonProgress::where('student_info_id', $student_info_id)
            ->sum('total_left_class_num');

dayinsql语句如下:

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();