1.Mysql遇见Expression #2 of SELECT list is not in GROUP BY clause
导致原因:MySQL 5.7.5后only_full_group_by成为sql_mode的默认选项之一,这可能导致一些sql语句失效。
解决方式一:
把group by字段group_id
设成primary key 或者 unique NOT NULL。这个方法在实际操作中没什么意义。(ps:没尝试)
解决方式二:
使用函数any_value
把报错的字段name
包含起来;(ps:尝试过,解决问题) (未尝试)例:
//原代码 select name, group_id from game group by group_id
报错信息:
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'game.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
解决:
select any_value(name), group_id from game group by group_id
解决方式三:
在配置文件my.cnf中关闭sql_mode=ONLY_FULL_GROUP_BY.msqyl的默认配置是
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
去掉,也可以去掉所有选项设置成sql_mode=
,如果你确信其他选项不会造成影响的话。
成功步骤:
命令行打开mysql.cnf,可以用whereis查找sudo vim /etc/mysql/conf.d/mysql.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
wq保存并退出
重启MySQL
解决方式四:
打开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";