我有一个雄辩的疑问:
return DB::table('users')
->leftJoin('bet', function($join) use ($competition_id){
$join->on('users.id', '=', 'bet.user_id');
$join->on('bet.competition_id','=', DB::raw("$competition_id"));
})
->select('users.*', DB::raw('SUM(points) as score'))->get();
它会返回一个错误:
SQLSTATE42000:语法错误或访问冲突: 1140混合组列(MIN()、MAX()、COUNT()、.)如果没有GROUP BY子句,则列是非法的(SQL:选择和(分数)从** users
左加入 bet
on users
**.**id
= bet
**.**user_id
和 bet
**.**competition_id
= 1E 239
)
如果我只是在PhpMyAdmin中复制粘贴生成的SQL (粗体),它就能很好地工作.
你能帮我解决这个问题吗?因为这对我来说毫无意义..。
谢谢
溶液
问题只是失踪的一群人:
return DB::table('users')
->leftJoin('bet', function($join) use ($competition_id){
$join->on('users.id', '=', 'bet.user_id');
$join->on('bet.competition_id','=', DB::raw("$competition_id"));
})
->select('users.*', DB::raw('SUM(points) as score'))->groupBy('users.id')->get();
解决方案#2
根据MySQL的版本或配置,上面的解决方案不起作用,我不得不按照Eric的建议groupBy所有字段:
return DB::table('users')
->leftJoin('bet', function ($join) use ($competition_id) {
$join->on('users.id', '=', 'bet.user_id');
$join->on('bet.competition_id', '=', DB::raw("$competition_id"));
})
->select('users.*', DB::raw('SUM(points) as score'))
->groupBy('users.id')
->groupBy('users.name')
->groupBy('users.firstName')
->groupBy('users.lastName')
->groupBy('users.email')
->groupBy('users.admin')
->groupBy('users.password')
->groupBy('users.remember_token')
->groupBy('users.created_at')
->groupBy('users.updated_at')->get();
}
发布于 2018-03-08 13:09:42
我有很多问题,但原因是
group by
上放置MySql或禁用严格模式来做到这一点。试试这个:
return DB::table('users')
->leftJoin('bet', function($join) use ($competition_id){
$join->on('users.id', '=', 'bet.user_id');
$join->on('bet.competition_id','=', DB::raw("$competition_id"));
})
->select('users.*', DB::raw('SUM(points) as score'))->groupBy('users.id')->get();
https://stackoverflow.com/questions/49182326
复制