- sql 报错:
Cause: java.sql.SQLSyntaxErrorException: Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ev_data_transmission.p.push_type' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ev_data_transmission.p.push_type' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
- 解释:这个错误是由于 MySQL 的 sql_mode 中包含 only_full_group_by 模式导致的。在 only_full_group_by 模式下,SELECT 列表中的非聚合列必须出现在 GROUP BY 子句中,或者该非聚合列必须是 GROUP BY 列的函数依赖。
当前的 SQL 查询中,SELECT 列表里有一些非聚合列没有出现在 GROUP BY 子句中,比如 p.push_type、p.push_mode 等,这就违反了 only_full_group_by 模式的规则。 - 解决方案
方法一:把非聚合列添加到 GROUP BY 子句中,就是将select中的查询参数全部放入 group by当中 。
方法二:关闭 only_full_group_by 模式
- 可以选择关闭 only_full_group_by 模式,但这种做法不推荐,因为 only_full_group_by 模式有助于保证查询结果的准确性。
你可以通过以下 SQL 语句临时关闭该模式:
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
- 永久关闭该模式,需要修改 MySQL 的配置文件(通常是 my.cnf 或者 my.ini),在 [mysqld] 部分添加或者修改 sql_mode 配置:
[mysqld]
sql_mode = "STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE,NO_ZERO_IN_DATE"
- 重启mysql
sudo systemctl restart mysqld
- 可以选择关闭 only_full_group_by 模式,但这种做法不推荐,因为 only_full_group_by 模式有助于保证查询结果的准确性。
验证 sql_mode 是否生效:
SELECT @@sql_mode;
如果输出中不再包含 ONLY_FULL_GROUP_BY,则修改成功。