[英]Laravel leftJoin and count issue
此代碼返回“ count ”和“ odds ”值完全相同(它們不應該相同)。 它實際上只計算“ o.id ”並為兩者返回相同的值。
我怎樣才能正確計算“b.id”?
\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();
更換:
COUNT(o.id) as odds,
COUNT(b.id) as count
有:
COUNT(DISTINCT(o.id)) as odds,
COUNT(DISTINCT(b.id)) as count
它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.