我已经加入了2个数据库,我想要的是获得这2个相乘列的总价值/总和:
数据库示例:
价格 数量 100 1 110 2 120 3
在我在 Laravel 中的查询中 - 我得到了总数 100、220、360,我想要所有这些的总和,总数等于 680,我怎样才能达到 680 总数?
$jointwo = DB::table('invoices')
->select('invoices.id', 'invoices.user', 'invoices.company', 'invoices.invoice_month', 'invoices.created','invoice_details.invoice_id', DB::raw('invoice_details.quantity * invoice_details.unit_price as total'))->join('invoice_details', 'invoice_details.invoice_id', '=', 'invoices.id')->get();
dd($jointwo);
我得到了总数100、220、360,我想要所有这些的总和,总数等于680,我怎样才能达到680总数?
您可以使用求和函数。
$jointwo = DB::table('invoices')
->select('invoices.id', 'invoices.user', 'invoices.company', 'invoices.invoice_month', 'invoices.created', 'invoice_details.invoice_id', DB::raw('invoice_details.quantity * invoice_details.unit_price as total'))
->join('invoice_details', 'invoice_details.invoice_id', '=', 'invoices.id')
->get();
// total sum of the 'total' column
$totalSum = $jointwo->sum('total');
dd($totalSum);