Mysql遇到的大坑:Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggre
报错如下
Expression #2 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'sss.month_id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
第一次遇到这样的问题,这是由于MySQL启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下)。说实话,我当时慌了,百度,谷歌搜了很多办法,如修改mysql的my.ini配置文件等,但是并没有什么用,如果你也遇到这样的问题,也没有解决,不妨试试我的方法。
打开Navicat或者SQLyog或者其他数据库管理工具,执行如下命令:
select @@global.sql_mode
你会看到查询出来的字符串中,有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';
执行完后,应该已经成功了,不妨再使用select @@global.sql_mode查看一下。
成功结果如图:
评论区