• #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column..........this is incompatible with sql_mode=only_full_group_by


    报错信息:

    Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'XXX' 
    which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

    原因:

    一、

    这个错误发生在mysql 5.7 版本及以上版本会出现的问题:

    mysql 5.7版本默认的sql配置是:sql_mode="ONLY_FULL_GROUP_BY",这个配置严格执行了"SQL92标准"。

    很多从5.6升级到5.7时,为了语法兼容,大部分都会选择调整sql_mode,使其保持跟5.6一致,为了尽量兼容程序。
    二、

    输出的结果是叫target list,就是select后面跟着的字段,还有一个地方group by column,就是

    group by后面跟着的字段。由于开启了ONLY_FULL_GROUP_BY的设置,所以如果一个字段没有在target list

    和group by字段中同时出现,或者是聚合函数的值的话,那么这条sql查询是被mysql认为非法的,会报错误。

    查看sql_mode

    select @@GLOBAL.sql_mode;

    解决方式一:

    暂时性修改sql_mode(重启mysql数据库服务之后,ONLY_FULL_GROUP_BY还会出现。)

    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

    mysql8 执行会报错:

    set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
    > 1231 - Variable 'sql_mode' can't be set to the value of 'NO_AUTO_CREATE_USER'

    原因:MySQL8取消了NO_AUTO_CREATE_USER

    把执行语句中的里的 'NO_AUTO_CREATE_USER'删掉

    解决方式二:找到mysql 配置文件

    [mysqld][mysql]下添加
    sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  • 相关阅读:
    资源与锁
    资源与锁
    Leetcode-Rotate List
    Leetcode-Unique Paths II
    Leetcode-Unique Paths
    Leetcode-Minimum Path Sum
    Leetcode-Sqrt(x)
    Leetcode-Set Matrix Zeroes
    Leetcode-Search a 2D Matrix
    Leetcode-Combinations
  • 原文地址:https://www.cnblogs.com/wongzzh/p/15303934.html
Copyright © 2020-2023  润新知