【问题标题】:Summing over multiple fields in Laravel在 Laravel 中对多个字段求和
【发布时间】:2013-03-12 18:17:59
【问题描述】:

我想知道是否可以使用流畅的查询构建器在一个查询中获取多个字段的总和。

我目前有两张桌子:活动和与会者。参加者属于活动并有两个字段:total_raised 和 total_hours。我想要做的是选择所有事件以及在该事件上花费的总金额/总小时数。现在,如果我只是使用 SQL,我会做一些事情:

 SELECT Event.id, sum(Attendees.total_raised), sum(Attendees.total_hours)
 FROM Events JOIN Attendees ON Events.id = Attendees.event_id 
 GROUP BY Event.id

但是,我似乎无法找到一种使用流利的查询生成器一次获取多个总和的方法。有什么方法可以使用 fluent 来做我想做的事情,还是应该把它变成一个原始的 SQL 查询?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您可以使用sum() 即:

    $q = DB::table('events')
           ->join('attendees', 'events.id', '=', 'attendees.event_id')
           ->sum('total_raised')
           ->sum('total_hours');
    

    如果还是不行你可以试试:

    ...
    
    ->get(
      array(
        'events.id',
        DB::raw('SUM(attendees.total_raised)'),
        DB::raw('SUM(attendees.total_hours)')
      )
    );
    

    【讨论】:

    • 这是我最初的想法,但是当我这样做时,我在第二次 sum 调用中遇到错误,上面写着“试图在非成员对象上调用 sum()”。
    • 知道了,我最终使用了该解决方案,尽管我确实必须添加一个小附录,因为我遇到了 DB 前缀未包含在您的答案中的问题。感谢您的帮助。
    • 不正确的答案不应该被标记和投票。您不能使用多个聚合函数。就像你不能做“->get()->all()”或“->sum()->get()”一样。这些函数不返回查询对象。
    【解决方案2】:

    以西蒙斯的回答为基础。您可以通过基本上运行两个查询来做到这一点。

    $query = DB::table('events')->join('attendees', 'events.id', '=', 'attendees.event_id');
    
    $raised = $query->sum( 'total_raised' );
    
    $hours = $query->sum( 'total_hours' );
    

    视情况而定。如果它在管理/CMS方面,我会倾向于这个解决方案。如果它在前端,它应该在一个查询中完成,这样会更快。根据内容的不同,它可能会或可能不会有显着差异。

    $result = DB::table('events')->join('attendees', 'events.id', '=', 'attendees.event_id')
        ->get( array(
            DB::raw( 'SUM(attendees.total_raised) AS raised' ),
            DB::raw( 'SUM(attendees.total_hours) AS hours' ),
        ));
    

    【讨论】:

      【解决方案3】:

      我在我的项目中做同样的事情,这是我找到的解决方案。我使用的是 Laravel 5.2 Eloquent,这里是 Eloquent 语句。

      我在项目中使用的这个语句,请根据您的需要进行更改。

      $result = self::select("*", DB::raw('SUM(auction_amount) as total_auction_amount') , DB::raw('SUM(commission_amount) as total_commission_amount'), 
                  DB::raw('SUM(deposit_amount) as total_deposit_amount'))
                  ->groupBy('cp_user_id')
                  ->get()
                  ->toArray();
      

      您可以使用相同的方式进行查询,例如

      $result = self::select("*", DB::raw('SUM(auction_amount) as total_auction_amount') , DB::raw('SUM(Attendees.total_raised) as total_raised'), 
                  DB::raw('SUM(Attendees.total_hours) as total_hours'))
                  ->with('Attendees')
                  ->groupBy('id')
                  ->get()
                  ->toArray();
      

      【讨论】:

        【解决方案4】:

        我写这个答案是为了帮助那些正在搜索的人在一个表中汇总多个字段。


        如果您想对单个表中的多个字段求和,这样就不需要“加入”,您可以简单地进行同样的操作,假设表是这样的。

        在您的控制器中执行以下操作:

        $billInfo= Bills::where('reports_id',2)->get( array(
                DB::raw('SUM(Price) as total_price'),
                DB::raw('SUM(balance) as total_balance'),
                DB::raw('SUM(paid) as total_paid'),
              ));
        

        这将产生以下数据:

        [{"total_price":17500,"total_balance":17500,"total_paid":null}]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-28
          • 1970-01-01
          • 1970-01-01
          • 2018-06-14
          • 1970-01-01
          • 2019-07-26
          • 1970-01-01
          相关资源
          最近更新 更多