laravel group by 42000

sql 分组查询异常:

sql error:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggr

连接数据时候使用了 ONLY_FULL_GROUP_BY 模式,下面是两种处理方式

1 , database.php  strict 关闭

strict=>false,

2 , 设置连接具体模式,strict 下添加 modes

 

strict=>true,
'modes'          => [
    'STRICT_TRANS_TABLES',
    'NO_ZERO_IN_DATE',
    'NO_ZERO_DATE',
    'ERROR_FOR_DIVISION_BY_ZERO',
    'NO_AUTO_CREATE_USER',
    'NO_ENGINE_SUBSTITUTION'
],
larave 连接mysql源码片段
/**
     * Set the modes for the connection.
     *
     * @param  PDO  $connection
     * @param  array  $config
     * @return void
     */
    protected function setModes(PDO $connection, array $config)
    {
        if (isset($config['modes'])) {
            $this->setCustomModes($connection, $config);
        } elseif (isset($config['strict'])) {
            if ($config['strict']) {
                $connection->prepare($this->strictMode($connection))->execute();
            } else {
                $connection->prepare("set session sql_mode='NO_ENGINE_SUBSTITUTION'")->execute();
            }
        }
    }

参考来源:

https://learnku.com/articles/6087/laravel-database-the-startup-and-connection-of-database-services

原文地址:https://www.cnblogs.com/heheisme/p/11497387.html