软件环境
MySQL版本:5.7.22
问题
错误代码: 1055
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'my_db.d.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
OR
错误代码: 1055
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'my_db.u.avatar' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
查询MySQL的sql mode
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
貌似在linux下安装的MySQL默认就是这种设置。(即使在/etc/my.cnf中根本没有sql_mode的设置)。
设置sql mode
windows下,查询my.ini文件,添加或修改
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'
linux下,查找/etc/my.cnf,添加或修改
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
windows下,貌似经常没有“ONLY_FULL_GROUP_BY”,导致有的sql在本地测试没有问题,部署后却出现问题。
为避免此情况,开发前确认本地MySQL的sql_mode与服务器一致。