解决MySQL5.7报SQLSTATE 1055的only_full_group_by问题
转:https://blog.csdn.net/huangyw888/article/details/80239216
Linux下,解决MySQL5.7报SQLSTATE 1055的only_full_group_by问题
2018年05月08日 15:15:49 four阿四 阅读数:358 标签: MySQLonly_full_group_by 更多
个人分类: MySQL
有的服务器上的MySQL会报如下的错误:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'bocai.bc_user_active.roomId' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
解决办法:
第一步:
mysql -u root -p
select @@sql_mode;
记录下此时sql_mode的值,如
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
不需要ONLY_FULL_GROUP_BY
第二步:
在my.cnf文件中(一般在/etc下)的[mysqld]下加一行:
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
或者在文件最后加两行:
-
[mysqld]
-
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
重启
/etc/init.d/mysql restart
注:
如果在
select @@sql_mode;
之后使用下面这个命令
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
或者这个命令
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';
只会临时更改sql_mode的值,重启MySQL后,又会恢复到原来的ONLY_FULL_GROUP_BY,所以要根据上面两步来更改my.cnf的配置。
其实Windows下也一样,不过配置文件名为my.ini。