千家信息网

mysql异常ERROR 1055 (42000)说明

发表于:2024-11-11 作者:千家信息网编辑
千家信息网最后更新 2024年11月11日,具体异常信息如下:ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains non
千家信息网最后更新 2024年11月11日mysql异常ERROR 1055 (42000)说明

具体异常信息如下:

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'aaa.test.age' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by


执行这样的SQL语句可重现异常 select age, name from test group by name

mysql5.1,不报异常

mysql5.5,不报异常

mysql5.6,不报异常

mysql5.7.22,报异常(具体从5.7哪个版本开始还没有确定下来,网上有人说5.7.5


mysql从5.7的某个版本开始调整了默认参数sql_mode值为:

mysql> show variables like 'sql_mode';+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+| Variable_name | Value                                                                                                                                     |+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+| 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 |+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)mysql>

其中ONLY_FULL_GROUP_BY就是影响了上面SQL语句是否报异常


解决方法:

1、改写SQL语句,毕竟上面的写法不是标准的SQL语句

2、登陆mysql服务器,执行以下命令,在global与session级都修改

mysql> 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';mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

3、结合第2种方法,在my.cnf文件中,指定sql_mode的值:

[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


注:2与3结合可不用重启服务器,且即使重启配置也依然有效。

0