Mysql group by 查询报错 1055 this is incompatible with sql_mode=only_full_group_by


一、问题

mysql8 使用 group by 查询时报错:
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘dkia_quality_db.b.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by


二、原因

  1. 当 mysql 配置文件 my.cnf 中的 sql_mode 字段包含 only_full_group_by 时 ,select 查询的字段与 group by 后的字段不一致就会报错
  2. mysql5.7.5 及以上的版本才会报这个错

三、解决办法

  1. 查看 sql_mode 配置
SELECT @@GLOBAL.sql_mode;

  1. 复制查询结果字符串并去掉 only_full_group_by
    在这里插入图片描述
    类似这样
    STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

  1. 找到 mysql 配置文件 my.cnf
    一般在 /etc/my.cnf 这个路径,当然也可能在其他自定义路径,比如我喜欢放在 /usr/mysql/conf/my.cnf

  1. 进入配置文件,修改配置
    mysqld 下增加 sql-mode
    在这里插入图片描述
    sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

  1. 保存,重启 mysql 服务
    我这边是 docker 安装的,所以直接 docker restart 容器id 即可

猜你喜欢

转载自blog.csdn.net/weixin_43721000/article/details/130423838
今日推荐