设置MySQL的sql_mode

软件环境

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与服务器一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值