問題描述
我在實現(xiàn) group_by 和在 Laravel 中使用 Eloquent 進(jìn)行查詢時遇到問題.
I am having trouble implementing the group_by and having queries using Eloquent in Laravel.
場景如下:
orders
- id
- qty
deliveries
- id
- qty
- order_id
我想使用連接來顯示未完成交貨的訂單以及相應(yīng)的余額:
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' 子句時,結(jié)果表不顯示任何行.有人有什么想法嗎?
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 并執(zhí)行似乎有效的 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();
這篇關(guān)于使用 Eloquent 實現(xiàn) group_by 并在 Laravel 中擁有的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!