5
查詢生成器,我將如何使用查詢生成器在Laravel生成以下SQL語句:如何使用與SUM()列和GROUPBY
SELECT costType, sum(amountCost) AS amountCost
FROM `itemcosts`
WHERE itemid=2
GROUP BY costType
我已經試過幾件事情,但我不能讓sum()
列與重命名一起使用。
我最新的代碼:
$query = \DB::table('itemcosts');
$query->select(array('itemcosts.costType'));
$query->sum('itemcosts.amountCost');
$query->where('itemcosts.itemid', $id);
$query->groupBy('itemcosts.costType');
return $query->get();