关于mysql 使用GROUP BY 查询报错:
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'student_info.s.SNAME' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
通过查询错误,常见的解决方式有两种
方式一.
打开navcat,
用sql查询:
select @@GLOBAL.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
去掉ONLY_FULL_GROUP_BY,重新设置值。
set @@global.sql_mode
=’STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’;
解决方式二:
在mysql安装目录下:
C:\Program Files\MySQL\MySQL Server 8.0
修改my.ini 文件:
[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
但是,使用此方式后,通过命令启动mysql失败:
C:\Windows\system32>net start mysql
MySQL 服务正在启动 .
MySQL 服务无法启动。
原因是:
MYSQL8以上已经取消了NO_AUTO_CREATE_USER,sql_mode中不能包含这个。
去掉后,启动成功:
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
再次查询sql_mode:
select @@GLOBAL.sql_mode;
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
可以看到,改动已生效