我是laravel的新手,我想以 LARAVEL 格式编写以下SQL。
SELECT user, SUM(total_net_amount) AS total
FROM primary_invoices
GROUP BY user_id
预先感谢
答案 0 :(得分:0)
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()
答案 1 :(得分:0)
这是我一直在寻找的准确答案。我希望这会对其他人有所帮助。
$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();