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

Sal*_*ern 29 mysql ubuntu yii2

当我将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
Run Code Online (Sandbox Code Playgroud)

正在执行的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
Run Code Online (Sandbox Code Playgroud)

怎么解决我的问题?

chi*_*old 66

跑过:

sudo mysql -u root -p
Run Code Online (Sandbox Code Playgroud)

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

mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Run Code Online (Sandbox Code Playgroud)

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

  • 如何回滚? (2认同)

ped*_*dev 18

在使用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,
    ],
],
Run Code Online (Sandbox Code Playgroud)

  • 即使答案似乎有效,我也建议不要放置代码图像,而是放置代码本身。然后 OP 就可以复制/粘贴,而不会在复制过程中出现拼写错误的风险。 (2认同)

sca*_*dge 8

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

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

sum(opportunity.id), sum(opportunity_conditions.money),
Run Code Online (Sandbox Code Playgroud)

总和(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
Run Code Online (Sandbox Code Playgroud)

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

GROUP BY`opportunity`.`id`,   `profile_details`.`user_id`,`opportunity_conditions.money`,  
Run Code Online (Sandbox Code Playgroud)


Hum*_*rey 8

请复制此行并运行它。它为我工作。

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Run Code Online (Sandbox Code Playgroud)

YouTube视频来解决这个问题

  • 不幸的是错误再次返回,但再次运行命令修复了它。不知道为什么它回来了。 (2认同)
  • 我还必须将 `[mysqld] sql-mode=""` 添加到 /etc/mysql/my.cnf 因为如果没有它,重新启动 mysql 服务时问题会再次出现。 (2认同)

cro*_*com 6

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

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Run Code Online (Sandbox Code Playgroud)

并添加

[mysqld]
sql-mode=""
Run Code Online (Sandbox Code Playgroud)

然后重启

sudo systemctl restart mysql
Run Code Online (Sandbox Code Playgroud)

适用于Ubuntu 18.04。