dongqing6755 2013-06-07 04:29
浏览 170
已采纳

使用Eloquent实现group_by并在Laravel中使用

I am having trouble implementing the group_by and having queries using Eloquent in Laravel.

Here is the scenario:

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

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?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doufei1893 2013-06-14 01:37
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?