Home > Back-end >  Laravel Eloquent/DB: Multiple sum fields in grouped select
Laravel Eloquent/DB: Multiple sum fields in grouped select

Time:03-04

Is there any possibility to do the following simple sql-select in Laravel?

SELECT a,b,c,d, SUM(e) as e, SUM(f) as f, SUM(g) as g FROM my_table GROUP BY a,b,c,d

CodePudding user response:

You could use the DB class for that:

DB::table('my_table')
    ->select('a', 'b', 'c', 'd', DB::raw('SUM(e) as e'), DB::raw('SUM(f) as f'), DB::raw('SUM(g) as g'))
    ->groupBy('a', 'b', 'c', 'd')
    ->get()

Documentation

  • Related