我有某种数据
order:
id name date payment
----------------------------------------
1 Amy's 2019-1-15 Cash
2 Hotel1's 2019-1-18 Visa
orderdetail:
id orderid food number
----------------------------------------------
1 1 1 3
2 1 2 1
3 1 3 3
4 1 4 2
5 2 1 1
6 2 3 1
food:
food name type fee
-------------------------------------------
1 Apple 1 4
2 Beef 2 15
3 Orange 1 3
4 Apple pie 1 7
我使用此sql查询获取了此数据(但未实现我的结果,缺少type1的子成本)
Order::query()
->select('order.name', 'order.date', DB::raw('(CASE WHEN food.type = 1 THEN SUM(orderdetail.number*food.fee) END) AS type1subcost'),'orderdetail.number*food.fee as totalcost', 'order.payment')
->leftJoin('orderdetail', 'Order.id', '=', 'orderdetail.orderid')
->leftJoin('food', 'order.food', '=', 'food.id')
->get();
我得到了什么:
order name/food name date type1subcost totalcost payment
----------------------------------------------------------------------------
order1--> Amy's 2019-1-15 35 50 Cash
Amy's 2019-1-15 50 Cash
order2--> Hotel1's 2019-1-18 7 7 Visa
我想得到如下结果:
order name/food name date type1subcost totalcost payment
----------------------------------------------------------------------------
order1--> Amy's 2019-1-15 35 50 Cash
order2--> Hotel1's 2019-1-18 7 7 Visa
,我只想要一个sql的结果。这是获得我的结果的更好方法?