這個(gè)例子是對(duì)課程進(jìn)度表里面的某個(gè)學(xué)生的剩余課時(shí)進(jìn)行求和匯總。
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
]
]
二、需要多個(gè)sum
laravel中怎么實(shí)現(xiàn)下面的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();
以上這篇Laravel 對(duì)某一列進(jìn)行篩選然后求和sum()的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Laravel統(tǒng)計(jì)一段時(shí)間間隔的數(shù)據(jù)方法
- Laravel模糊查詢區(qū)分大小寫的實(shí)例
- php數(shù)組函數(shù)序列之a(chǎn)rray_sum() - 計(jì)算數(shù)組元素值之和