Sql 在LARAVEL中使用group by子句检索列的和
我是laravel的新手,我想以laravel格式编写以下SQLSql 在LARAVEL中使用group by子句检索列的和,sql,laravel,Sql,Laravel,我是laravel的新手,我想以laravel格式编写以下SQL SELECT user, SUM(total_net_amount) AS total FROM primary_invoices GROUP BY user_id 提前谢谢 PrimaryInvoice::selectRaw('SUM(total_net_amount) as total') ->groupBy('user_id') ->get() 如果primary_invoices与us
SELECT user, SUM(total_net_amount) AS total
FROM primary_invoices
GROUP BY user_id
提前谢谢
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()
这正是我想要的答案。我希望这能帮助其他人
$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();
那个
DESC
不应该在那里。(它属于ORDER BY。)通常按所选列进行分组,但作为设置函数参数的列除外。也就是说,在这里按用户分组。你能告诉我们你自己的Laravel代码尝试吗?你有答案,但这些链接将有助于我们向前迈进:和