mysql - Laravel 3: Joining and getting sum of two tables, Is there a better way? -
i'm trying columns of table while getting sum of related table (i'm using mysql database). tables advantages , conversions, advantages has conversionid
column join. related via models. first tried achieve eloquent method, not succeed, came fluent method, working fine:
db::table('conversions') ->join('advantages','advantages.conversionid','=','conversions.id') ->where('conversions.used','=',0) ->group_by('conversions.id') ->get(array( 'conversions.*', db::raw('sum(advantages.point) totaladvantage') ))
i guess query describes how columns , want achieve.
so question is: there more efficient way achieve this? using db::raw
seemed weird me, , sum()
method returns sum of columns. place wrote both fluent , raw query in project, made me think if i'm missing something.
thanks in advance
you can call aggregate methods through fluent
db::table('conversions') ->join('advantages','advantages.conversionid','=','conversions.id') ->where('conversions.used','=',0) ->group_by('conversions.id') ->select(array( 'conversions.*', 'advantages.point totaladvantage') ) ->sum('advantages.point')
havent tested this, let me know how on