[英]Retrieving a sum of a column with group by clause in LARAVEL
我是 Laravel 的新手,我想以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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.