当前位置: 首页>>技术教程>>正文


SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT列表的表达式#3不在GROUP BY子句中并包含nonaggregated

, ,

问题描述

当我将ubuntu从15.10升级到16.04时,我在yii2项目中有这个错误

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3
of SELECT list is not in GROUP BY clause and contains nonaggregated column 
'iicityYii.opportunity_conditions.money' which is not functionally dependent 
on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

正在执行的SQL是:

SELECT SUM(oc.money),op.id,oc.money,
            op.mantaghe,
            op.`time`, op.`id`, `op`.`logo`,
           `pd`.`user_id`, `op`.`name`, 
           `pd`.`co_name`, `op`.`address`, 
           `op`.`project_type_id`, `op`.`state_id`
FROM `opportunity` op 
INNER JOIN `profile_details` pd  ON op.user_id=pd.user_id  
INNER JOIN `opportunity_conditions` oc ON   op.id=oc.opportunity_id
GROUP BY `op`.`id`
ORDER BY `op`.`id` DESC

怎么解决我的问题?

最佳解决思路

在您的选择中,您有一个聚合函数和一组列名称,该错误告诉您没有在group by子句中指定正确的列名列表。可能是你应该在组中添加更多的列名称,可能与profile_details, opportunity_conditions表有关

您还有,(opportunity.id),(opportunity_conditions.money),
(opportunity.mantaghe),
为什么()如果您需要总和,您必须向所有列添加总和

sum(opportunity.id), sum(opportunity_conditions.money),

总和(opportunity.mantaghe),

否则,如果这是正常的列,你应该使用正常的sintax没有()

opportunity.id, opportunity_conditions.money,opportunity.mantaghe,

我试图重写一个可能的查询

 SELECT SUM(opportunity_conditions.money),
        `opportunity`.`id`,
        `opportunity_conditions.money`,
        `opportunity.mantaghe`, 
        `opportunity`.`time`, 
        `opportunity`.`logo`, 
        `profile_details`.`user_id`,
        `opportunity`.`name`, 
        `profile_details`.`co_name`,
        `opportunity`.`address`, 
        `opportunity`.`project_type_id`,
        `opportunity`.`state_id` 
FROM `opportunity` 
INNER JOIN `profile_details` ON `opportunity`.`user_id`= `profile_details`.`user_id` 7
INNER JOIN `opportunity_conditions` ON `opportunity`.`id`=`opportunity_conditions`.`opportunity_id` 
GROUP BY`opportunity`.`id`,   `profile_details`.`user_id`,`opportunity_conditions.money`,  
ORDER BY `opportunity`.`id` DESC

与基本列名称分组(我希望)

GROUP BY`opportunity`.`id`,   `profile_details`.`user_id`,`opportunity_conditions.money`,  

次佳解决思路

跑过:

sudo mysql -u root -p

并更改MySQL服务器实例的SQL模式:

mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

另一种方法是使用MySQL配置。转到/etc/mysql/my.cnf

  • [mysqld]添加一个部分,在它下面添加语句sql_mode = ""

  • 重启mysql服务:

    sudo systemctl restart mysql
    

第三种解决思路

在使用MySql的laravel中,转到文件config /database.php,它将数组MySql mode strict更改为false。

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => false, //from true
        'engine' => null,
    ],
],

第四种思路

请复制此行并运行它。它对我有用。

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

Youtube video to fix this

第五种思路

解决方案是编辑MySQL配置文件,因为配置将在每次重启后恢复…

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

并添加

[mysqld]
sql-mode=""

然后重启

sudo systemctl restart mysql

适用于ubuntu 18.04。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/6280.html,未经允许,请勿转载。