表格数据:
id || product_id || quantity || sold_price || image (Relationship)
1 || 1 || 10 || 10 || 1.png
2 || 2 || 5 || 20 || 2.png
3 || 1 || 15 || 15 || 1.png
期望的返回数据
重复的product_id
和25
的平均售价为sold_price
要获得平均值,我们将得到10+15/2 = 2
。
预期结果
sum/counter
我尝试过的
id || product_id || quantity || sold_price || image (Relationship)
1 || 1 || 25 || 12.5 || 1.png
2 || 2 || 5 || 20 || 2.png
但是这只返回$products = SoldProducts::whereIn('export_invoice_id',$export_invoice_ids)
->groupBy(DB::raw('product_id'))
->select([
'sold_price',DB::raw('sum(quantity) total_quantity'),])
-> get();
和total_quantity
,而没有product_id
。
问题
问题是id,average_sold_price,image
来自某个关系。
我尝试使用image
,但是选择不起作用。