在Laravel执行group_by和拥有使用雄辩 [英] Implementing group_by and having in Laravel using Eloquent

查看:651
本文介绍了在Laravel执行group_by和拥有使用雄辩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Laravel执行group_by并使用Enloquent进行查询。

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();

如果没有'having'子句,'balance'值正常工作。然而,在添加'having'子句时,生成的表不显示任何行。有人有任何想法吗?

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();

这篇关于在Laravel执行group_by和拥有使用雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆