【问题标题】:Query works in phpMyAdmin, but not in Laravel [duplicate]查询在 phpMyAdmin 中有效,但在 Laravel 中无效 [重复]
【发布时间】: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 from tasks as @987654325 @ left join users as u on t.empid = u.id where u.role = user group by t.empid)

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    这可能是 SQL_MODE 的问题。

    在你的config/database.php中,在连接中,改变mysql

    strict => false
    

    【讨论】:

      【解决方案2】:

      在 config/database.php 中的“mysql”更改:

      'strict' => true,
      

      为假。 希望对您有所帮助!

      【讨论】:

        【解决方案3】:

        感谢您的回答 - 我在代码中更改了列名,它可以工作

        $allemp = DB::table('users as u')
                                ->join('tasks as t','t.empid','=','u.id')
                                ->where('u.role','=','user')
                                ->select('u.id','u.name','u.email',DB::raw('count(t.empid) as totalassignedtask'))
                                ->groupBy('u.id','u.name','u.email')
                                ->orderBy('totalassignedtask', 'asc')
                                ->get();
        

        【讨论】:

          猜你喜欢
          • 2014-10-31
          • 2013-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-10
          • 1970-01-01
          相关资源
          最近更新 更多