jeudi 26 janvier 2017

Laravel leftJoin and count issue

This code returns "count" and "odds" values exactly same (They should not be same). It actually only counts the "o.id" and returns the same value for both.

How can I count "b.id" correctly?

    \DB::table('matches as m')
  ->selectRaw('     m.id as match_id, 
                    m.date_hour as date, 
                    m.tournament_id as tournament_id,
                    h.name as host_name, 
                    g.name as guest_name, 
                    COUNT(o.id) as odds,
                    COUNT(b.id) as count
                     ')
  ->whereRaw('DATE(m.date_hour) = DATE(NOW())') //OK 
  ->leftJoin('teams as h','h.id','=','m.host_id')
  ->leftJoin('teams as g','g.id','=','m.guest_id')
  ->leftJoin('odds as o','o.match_id','=','m.id')
  ->leftJoin('bets as b','b.match_id','=','m.id') 
  ->groupBy('m.id')
  ->having('odds','>','0')
  ->get();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire