[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
sql _mode
中only _full _group _by
不兼容的问题
$ SHOW VARIABLES LIKE "sql_mode";
$ SET sql_mode='';
$ SHOW VARIABLES LIKE "sql_mode";
$ SHOW VARIABLES LIKE "sql_mode";
$ SET sql_mode='NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES';
$ SHOW VARIABLES LIKE "sql_mode";
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column xxx which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
- 错误原因
对于GOUP BY
聚合操作,如果在SELECT
中列没有在GROUP BY
中出现则认为SQL是不合法的,因为列不在GROUP BY
子句中所以设置了sql_mode=only_full_group_by
的数据库,在使用GROUP BY
时会报错。
- 错误查询
mysql> SELECT VERSION();
+------------+
| VERSION() |
+------------+
| 5.7.22-log |
+------------+
1 row in set
mysql> SELECT @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
查看当前sql_mode
配置中是否出现ONLY_FULL_GROUP_BY
选项
- 解决方案
永久去除:从MySQL主配置文件my.cnf
的sql_mode
选项中剔除ONLY_FULL_GROUP_BY
选项
$ vim /etc/my.cnf
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
$ service mysqld restart
临时去除:在MySQL命令行执行设置sql_mode
选项,选项中不包含ONLY_FULL_GROUP_BY
。
mysql> SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected