Mysql (ONLY_FULL_GROUP_BY) Expression #1 of SELECT list is not in GROUP BY ...

306 阅读1分钟

Mysql5.7 用 group by 出现如下报错:

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

原因:是在 Mysql5.7 下 sql_mode 默认为 ONLY_FULL_GROUP_BY

  • windows 解决方案

  • mac 解决方案:

    1、查看 sql_mode:

    show session variables;
    

    找到里面的 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
    

    2、将输出的 sql_mode 值去掉 ONLY_FULL_GROUP_BY 之后就是这样的:

    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、找到我们配置 mysql 的 my.cnf 文件,如果不知道怎么配置 my.cnf 文件,可以看看我这篇文章 Mac MySql - 查看以及修改编码格式

    4、找到 /etc 目录下的 my.cnf 文件之后,拷贝到桌面用文本编辑器打开,将下面的配置拷贝进去

    # 设置模式
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    

    5、将桌面上的 my.cnf 文件拷贝到 /etc 文件夹里面覆盖里面的 my.cnf 文件,然后重启Mysql即可。

    show session variables;
    

    再次输出就是新配置的 sql_mode 了!