Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.fruits.f_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
时间: 2023-07-14 19:14:27 浏览: 70
这个错误信息是因为在 MySQL 5.7.5 以后的版本中,sql_mode 默认开启了 ONLY_FULL_GROUP_BY 模式,该模式要求 GROUP BY 后面的列必须在 SELECT 中出现,或者在 SELECT 中使用聚合函数进行计算。
如果你的查询语句中出现了类似上面的错误信息,有两种解决方法:
1. 将 sql_mode 设置为非 only_full_group_by 模式,可以通过下面的语句实现:
```
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
```
2. 在 GROUP BY 后面的列中加入所有 SELECT 中出现的列,或者使用聚合函数进行计算,例如:
```
SELECT fruits.f_id, fruits.f_name, COUNT(*) FROM fruits GROUP BY fruits.f_id, fruits.f_name;
```
相关问题
MySQL解决报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column. macbook怎么解决这个报错
在MySQL中,当使用GROUP BY子句对结果进行分组时,SELECT列表中的列必须要么是分组的列,要么是聚合函数的结果。否则,会出现报错"Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column"。解决这个报错的方法有两种:
1. 使用聚合函数对非分组列进行聚合操作。
2. 修改MySQL的sql_mode参数,将其设置为非严格模式。
下面是两种解决方法的示例:
方法一:使用聚合函数对非分组列进行聚合操作
```sql
SELECT custname, MAX(salary) FROM your_table GROUP BY custname;
```
方法二:修改MySQL的sql_mode参数
```sql
SET sql_mode = 'ONLY_FULL_GROUP_BY,NO_AUTO_CREATE_USER';
```
请注意,修改sql_mode参数可能会影响其他查询的行为,因此在修改之前请确保了解其影响。
SELECT list is not in GROUP BY clause and contains nonaggregated column 'a.V_TENOR1' which is not functionally
SELECT list is not in GROUP BY clause and contains nonaggregated column 'a.V_TENOR1' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by.
这个错误通常在使用GROUP BY子句时出现。它表示SELECT列表中的某些列没有在GROUP BY子句中,并且这些列不是函数依赖于GROUP BY子句中的列。这与sql_mode=only_full_group_by不兼容。
解决这个问题的方法有两种:
1. 将非聚合列添加到GROUP BY子句中:将SELECT列表中的非聚合列添加到GROUP BY子句中,以确保所有列都被正确分组。
2. 使用聚合函数处理非聚合列:如果你不需要将非聚合列添加到GROUP BY子句中,可以使用聚合函数(如SUM、MAX、MIN等)对这些列进行处理,以便它们能够与GROUP BY子句兼容。