检索 LARAVEL 中带有分组依据子句的列的总和

  • 本文关键字:子句 LARAVEL 检索 sql laravel
  • 更新时间 :
  • 英文 :


我是拉拉维尔的新手,我想用拉拉维尔格式编写下面的SQL。

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

提前致谢

这是我正在寻找的确切答案。我希望这会帮助其他人。

$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();
PrimaryInvoice::selectRaw('SUM(total_net_amount) as total')
   ->groupBy('user_id')
   ->get()

如果primary_invoices与 users 表有关系,则可以使用如下所示的内容:

PrimaryInvoice::selectRaw('SUM(total_net_amount) as total, user_id')
   ->with('user')
   ->groupBy('user_id')
   ->get()

最新更新