gpt4 book ai didi

php - Laravel leftJoin 和计数问题

转载 作者:行者123 更新时间:2023-11-30 22:05:37 24 4
gpt4 key购买 nike

此代码返回“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

它应该可以工作。

关于php - Laravel leftJoin 和计数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com