本文介紹了使用 Eloquent 實現 group_by 并在 Laravel 中擁有的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在實現 group_by 和在 Laravel 中使用 Eloquent 進行查詢時遇到問題.
I am having trouble implementing the group_by and having queries using Eloquent in Laravel.
場景如下:
orders
- id
- qty
deliveries
- id
- qty
- order_id
我想使用連接來顯示未完成交貨的訂單以及相應的余額:
I want to use a join to display the orders with incomplete deliveries as well as the corresponging balance:
Order::left_join('deliveries', 'orders.id', '=', 'deliveries.order_id')
->select(array('orders.*'), DB::raw('orders.qty - IFNULL(sum(deliveries.qty),0) AS balance')))
->group_by('order_id')
->having('balance', '>', 0)
->get();
'balance' 值在沒有 'have' 子句的情況下也能正常工作.然而,在添加 'have' 子句時,結果表不顯示任何行.有人有什么想法嗎?
The 'balance' value works fine without the 'having' clause. On adding the 'having' clause however, the resulting table doesn't display any rows. Does anyone have any ideas?
提前致謝!
推薦答案
最終切換到 Laravel 4 并執行似乎有效的 ff.
Ended up switching to Laravel 4 and doing the ff which seemed to work.
Order::leftJoin('deliveries', 'orders.id', '=', 'deliveries.order_id')
->select(array('orders.*'), DB::raw('orders.qty - IFNULL(sum(deliveries.qty),0) AS balance')))
->groupBy('order_id')
->havingRaw('balance > 0')
->get();
這篇關于使用 Eloquent 實現 group_by 并在 Laravel 中擁有的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!