原因
聚合查询出现这样的报错是因为mysql设定的mode含有 ONLY_FULL_GROUP_BY
1
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 (0.00 sec)
在上面修改的基础上,本人成功的解决方法,
cd /etc/mysql/mysql.conf.d/
(有的mysql配置文件是 /etc/my.cnf ,而我的Ubuntu16.04 + mysql 5.7.24 下 mysql配置文件是这里 /etc/mysql/mysql.conf.d/)
sudo vim mysqld.cnf
在[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
如果是docker
要挂载docker容器目录/etc/mysql/mysql.conf.d
把原来的目录里面的文件mysqld.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
重启mysql
mysql报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregate