only_full_group_by

问题

Mysql 升级至5.7后发现在原来的有个 group by 语句报错了incompatible with sql_mode=only_full_group_by,一直以来都用的好好的,怎么就突然报错。原来是5.7新加了一些的检查更严格了。

select * from a group by etf_date;
which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决方案

A. 当前实例有效,重启失效

    SET sql_mode = ''; // 只针对当前客户端有效
    SET GLOBAL sql_mode = ''; // 全局有效

B. 永久生效

    mysql -u homestead -psecret -e "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 |
    +-------------------------------------------------------------------------------------------------------------------------------------------+

这个就是当前mysql生效的mode, 我们可以去掉我们不需要的一些mode,比如ONLY_FULL_GROUP_BY, NO_ZERO_IN_DATE, NO_ZERO_DATE等这些,然后编辑mysql的配置的文件(比如 /etc/mysql/my.cnf),在 [mysqld] 下加上:

[mysqld]
   sql_mode = "STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

然后保存,退出,最后重启Mysql

sudo service mysql restart

大功告成,这些sql的mode将会永久生效了。

# 技巧 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×