2017-02-28 29 views
0

我嘗試轉換下面的查詢使用查詢生成器來laravels但條款和金額具有組一些問題彙總功能有困難SQL查詢轉換爲Laravel建設者

SELECT code, SUM(quantity) as quantity, SUM(sale_price) as price 
FROM `orders` o 
GROUP By code 

我曾經嘗試這樣做,這是在經歷了這麼多絕望的嘗試之後,我有了什麼。

$args['orders'] = DB::table('orders') 
->havingRaw('sum(sale_price)') 
->havingRaw('code') 
->where([ 'shop_id' => $shop_id ]) 
->groupBy('code') 
->get(); 

回答

1

通過

$args['orders'] = DB::table('orders') 
->where('shop_id', '=', $shop_id) 
->select('code', DB::raw('sum(quantity) as quantity, sum(sale_price) as price')) 
->groupBy('code') 
->orderBy('quantity') 
->get(); 

$args['orders'] = DB::table('orders') 
->where('shop_id', '=', $shop_id) 
->select('code', DB::raw('sum(quantity) as quantity, sum(sale_price) as price')) 
->groupBy('code') 
->get(); 

訂單一次或嘗試

->orderByRaw('sum(quantity)') 
+0

我怎麼會在這個附加ORDER_BY列? – Waleed

+0

查看我的回答編輯 – EddyTheDove

+0

Nah,order_by實際上是一個將被連接到另一個表的列。 – Waleed