【发布时间】:2017-04-23 18:48:49
【问题描述】:
遇到一个奇怪的问题,我最初尝试过的地方
$usersWithAnswersCount = GameResult::where([
'school_id' => null,
'season_id' => $this->season->id
])
->groupBy('user_id')
->count();
我的桌子看起来像
但是,当它本应返回 "2" 时,我得到了结果 "1"
当我检查运行的查询是 select count(*) as aggregate from game_results where (school_id is null and season_id = '1') group by user_id - 如果我直接作为 mysql 查询运行返回 2!!
所以看起来 eloquent 中的某些东西正在将我的 2 更改为 1 :(
但是,如果我将其更改为 DB Query Builder 并写出
$usersWithAnswersCount = DB::table('game_results')
->selectRaw('count(distinct user_id) as count')
->where([
'school_id' => null,
'season_id' => $this->season->id
])
->first()->count;
我回复"2",这是我的预期。
但是我不清楚为什么 Eloquent 方法会失败,如果可能的话我可以做些什么来修复它。
【问题讨论】:
-
你是如何输出这些结果的?
-
混合使用
dump($usersWithAnswersCount);和dd($usersWithAnswersCount);
标签: php mysql laravel count eloquent