解决laravel出现Syntax error or access violation: 1055 ‘***‘ isn‘t in GROUP BY

laravel 5.3 以后默认开启 mysql严格模式(strict)
在mysql在严格模式下, 并且开启了ONLY_FULL_GROUP_BY的情况下,
group by 的字段没有出现在 select 的语句中会报错.关闭了严格模式就不会报错.

如下Demo

复制代码

public static function orders($phase)
{
    return self::select(DB::raw("SUM(orders) as orders"),"type","users_id","name")
                ->groupBy("type","name")
                ->where("phase",$phase)
                ->get();
}

会出现如下错误,当然*号内的内容是不同的

复制代码

Syntax error or access violation: 1055 '***' isn't in GROUP BY

解决办法

laravel出现上述错误是sql_mode设置导致的问题,修改config/database.php配置文件的strict => false即可关闭了严格模式:

复制代码

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
],
上一篇:信息学奥赛一本通(1055:判断闰年)


下一篇:mysql Error 1055错误