首页 > 解决方案 > 在 LARAVEL 中使用 group by 子句检索列的总和

问题描述

我是 laravel 的新手,我想以LARAVEL格式编写以下 SQL。

SELECT user, SUM(total_net_amount) AS total 
FROM primary_invoices  
GROUP BY user_id  

提前致谢

标签: sqllaravel

解决方案


这是我一直在寻找的确切答案。我希望这对其他人有帮助。

$report_dates = DB::table('primary_invoices')
->select('user','datee', DB::raw('SUM(total_net_amount) as total'))
->where(function ($query) use($date_value,$enddate_value) {
$query->whereBetween('datee', [$date_value, $enddate_value]);
})
->where('user',$search_value)
->groupBy('datee')
->orderBy('datee','asc')
->get();

推荐阅读