这个例子是对课程进度表里面的某个学生的剩余课时进行求和汇总。
laravel 版本是 lts 5.5
1
2
|
StudentLessonProgress::where( 'student_info_id' , $student_info_id ) ->sum( 'total_left_class_num' ); |
打印sql语句如下:
1
2
3
4
5
6
7
|
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……;
参考
1
2
3
4
5
6
|
Product::where( 'status' ,1)->first( array ( \DB::raw( 'SUM(profit) as profit' ), \DB::raw( 'SUM(order_count) as order_count' ) ) )->toArray(); |
以上这篇Laravel 对某一列进行筛选然后求和sum()的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zhezhebie/article/details/80322100