2017-01-26 70 views
1

此代碼返回「count」和「賠率」的值完全相同(它們不應該相同)。 它實際上只計算「o.id」並返回相同的值。Laravel left加入並計算問題

如何正確計算「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(); 
+0

請張貼使用過的數據庫表格的結構和一些你想要的東西。 – Jerodev

+0

@Jerodev我有表格:**匹配**,**賠率**與match_id,** **投注**與match_id。我想獲得與奇數和下注數列出的比賽。 – Rasko

回答

0

替換:

COUNT(o.id) as odds, 
COUNT(b.id) as count 

有:

COUNT(DISTINCT(o.id)) as odds, 
COUNT(DISTINCT(b.id)) as count 

它應該工作。

+0

是的,這對我有效。謝謝! – Rasko