【发布时间】:2019-02-06 03:20:40
【问题描述】:
如果我在 phpmyadmin 中编写以下查询,它会运行并返回正确的结果
select `u`.*, count(t.empid) as totalassignedtask from `users` as `u` left join `tasks` as `t` on `t`.`empid` = `u`.`id` where `u`.`role` = 'user' group by `t`.`empid`
但是,如果我像这样在 laravel 中编写此查询
$allemp = DB::table('users as u')
->leftJoin('tasks as t','t.empid','=','u.id')
->where('u.role','=','user')
->select('u.*',DB::raw('count(t.empid) as totalassignedtask'))
->groupBy('t.empid')
->get();
我收到一个错误:
Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]:语法错误或访问冲突:1055 'employee.u.id' 不在 GROUP BY 中(SQL:选择u.*,count(t.empid) as totalassignedtask fromtasksas @987654325 @ left joinusersasuont.empid=u.idwhereu.role= user group byt.empid)
【问题讨论】: