![](/img/trans.png)
[英]Laravel 5.4 Query Builder SUM() from two different tables
[英]Laravel Sum Eloquent sum of multiple multiplications from two different tables
目前,在我的 Laravel 項目控制器中,我使用了一個查詢
查詢 1
public function cartreview(Request $request,$sp_id, $service_id,$cart_id)
{
$total = DB::table('pricings')
->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.shirt*carts.quantity_shirt ) AS total'))
->where('pricings.sp_id', '=', $sp_id)
->where('carts.id', '=' , $cart_id)
->first();
}
在上面的查詢中,我使用兩個數據庫表作為定價和購物車,我通過從定價表中獲取價格和從購物車表中獲取數量來計算襯衫項目的總賬單價格。
現在我還想用褲子、領帶等襯衫添加另一個項目。如何將更多的乘法傳遞給總和?
請幫我解決語法問題。 我可以做這樣的事情嗎
查詢 2
$total = DB::table('pricings')
->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.shirt*carts.quantity_shirt ,
pricings.pant*carts.quantity_pant ,
pricings.tie*carts.quantity_tie) AS total'))
->where('pricings.sp_id', '=', $sp_id)
->where('carts.id', '=' , $cart_id)
->first();
或者即使我單獨計算每個項目的總數我如何添加它?
$total_shirt = DB::table('pricings')
->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.shirt*carts.quantity_shirt ) AS total_shirt'))
->where('pricings.sp_id', '=', $sp_id)
->where('carts.id', '=' , $cart_id)
->first();
$total_pant = DB::table('pricings')
->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.pant*carts.quantity_pant ) AS total_pant'))
->where('pricings.sp_id', '=', $sp_id)
->where('carts.id', '=' , $cart_id)
->first();
$total_tie = DB::table('pricings')
->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.tie*carts.quantity_tie ) AS total_tie'))
->where('pricings.sp_id', '=', $sp_id)
->where('carts.id', '=' , $cart_id)
->first();
$total = $total_shirt + $total_pant + $total_tie; ?
要在 view.blade.php 中顯示值,我使用類似 {{$total->total}}
提前致謝。
嘗試:
$waftotal = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(
pricings.Regular_Laundry*carts.q_Regular_Laundry,
pricings.Bedding_Mattress_Duvet_Cover*carts.q_Bedding_Mattress_Duvet_Cover,
pricings.Bedding_Comforter_laundry*carts.q_Bedding_Comforter_laundry,
pricings.Bedding_Blanket_Throw*carts.q_Bedding_Blanket_Throw,
pricings.Bedding_Pillow_laundry*carts.q_Bedding_Pillow_laundry,
pricings.Bath_Mat_laundry*carts.q_Bath_Mat_laundry,
pricings.Every_Hang_Dry_Item*carts.q_Every_Hang_Dry_Item
) AS waftotal'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
但它給了我錯誤
SQLSTATE[42000]:語法錯誤或訪問沖突:1064 你的 SQL 語法有錯誤; 檢查與您的 MariaDB 服務器版本相對應的手冊,以了解在第 1 行的“pricings.Bedding_Mattress_Duvet_Cover c”附近使用的正確語法(SQL:select sum( pricings.Regular_Laundrycarts.q_Regular_Laundry, pricings.Bedding_Mattress_Duvet_Cover carts.q_Bedding_Duvet_Cover carts.q_Bedding_Duvet_Cover c') .q_Bedding_Comforter_laundry,pricings.Bedding_Blanket_Throw carts.q_Bedding_Blanket_Throw,pricings.Bedding_Pillow_laundry carts.q_Bedding_Pillow_laundry,pricings.Bath_Mat_laundry carts.q_Bath_Mat_laundry,pricings.Every_Hang_Dry_Item carts.q_Every_Hang_Dry_Item)AS從waftotal pricings
內部聯接carts
上carts
。 sp_id
= pricings
。 sp_id
其中pricings
。 sp_id
= 1 和carts
id
= 23 限制 1)
即使我編寫單獨的查詢
$waf1 = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.Regular_Laundry*carts.q_Regular_Laundry) AS waf1'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
$waf2 = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.Bedding_Mattress_Duvet_Cover*carts.q_Bedding_Mattress_Duvet_Cover) AS waf2'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
$waf3 = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.Bedding_Comforter_laundry*carts.q_Bedding_Comforter_laundry) AS waf3'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
$waf4 = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.Bedding_Blanket_Throw*carts.q_Bedding_Blanket_Throw) AS waf4'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
$waf5 = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.Bedding_Pillow_laundry*carts.q_Bedding_Pillow_laundry) AS waf5'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
$waf6 = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.Bath_Mat_laundry*carts.q_Bath_Mat_laundry) AS waf6'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
$waf7 = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
->select(DB::raw('sum(pricings.Every_Hang_Dry_Item*carts.q_Every_Hang_Dry_Item) AS waf7'))
->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();
$waftotal = $waf1->waf1 + $waf2->waf2 + $waf3->waf3 + $waf4->waf4 + $waf5->waf5 + $waf6->waf6 + $waf7->waf7 ;
在 view.blade.php {{$waftotal}} 或 {{$waftotal->waftotal}} 中給我 eerror as Trying to get property of non-object 。
建議將不勝感激。
first()
將返回一個對象,因此您需要添加每個對象的屬性: https : //laravel.com/docs/5.6/eloquent#retrieving-single-models
而不是$total = $total_shirt + $total_pant + $total_tie
;
這將是$total = $total_shirt->total_shirt + $total_pant->total_pant + $total_tie->total_tie;
因為您已將每個對象的總和分配給以該對象命名的屬性。
但是您的第一個組合查詢應該可以正常工作。 你得到什么錯誤?
$maintotal = DB::table('carts')
->join('products', 'carts.productid', '=', 'products.id')
->select(DB::raw('sum(products.price*carts.quantity) AS maintotal3'))
->where('carts.userid', '=', $useri)->get();
@foreach($maintotal as $t)
{{$t->maintotal3}}
@endforeach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.