我有以下代码:
DB::table('product_list')->select(DB::raw('(value*quantity) as total'))->get();
这是我的数据库:
product = id,quantity,value,color
我希望将它们添加为:
1|10|500|blue
2|20|250|red
total for each = value*quantity
total = 10000
但这给了我:
array(2) { [0]=> object(stdClass)#266 (1) { ["total"]=> int(5000) } [1]=> object(stdClass)#263 (1) { ["total"]=> int(5000) } }
总结这一切的唯一方法是在PHP中运行一个foreach循环,但我需要直接从数据库中进行。但是必须有另一种方式,不是吗?
使用SQLSUM
函数:
$total = DB::table('product_list')
->selectRaw('SUM(value * quantity) as total')
->pluck('total');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句