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

吾爱主题 阅读:116 2021-08-27 15:39:00 评论:0

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

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

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容