zoukankan      html  css  js  c++  java
  • SELECT list is not in GROUP BY clause and contains nonaggregated column 't_user_rel.distance' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=on

    错误信息

    SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 
     't_user_rel.distance' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
     
    
    SQLSTATE[42000]: Syntax error or access violation: 1140 In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column
     't_team.uid'; this is incompatible with sql_mode=only_full_group_by
    
     
    

    发生错误的源sql语句

    SELECT `uid`,`distance` FROM `t_user_rel` WHERE  `uid` IN (7534,2967,2145,901,746,654,2) GROUP BY `uid` ORDER BY distance asc"
    
    

    其中distance 不是group by的字段 ,数据库做了限制 sql_mode = only_full_group_by,所以报错

    解决方法

    错误信息就是sql语句使用group by的时候 获取字段column 不是分组的字段,mysql配置做了限制: sql_mode=only_full_group_by

    网上查了很多的解决方法就是修改配置文件,就可以解决这个问题。

    参考:https://blog.csdn.net/qq_42175986/article/details/82384160

    解决方法就是把

    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

    设置成

    STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

    就是把分组查询字段不做限制

    查看数据库sql_mode的配置

    SHOW VARIABLES LIKE '%sql_mode%'

    yum指定官方源安装好的不同mysql版本的默认sql_mode 的配置如下:
    MySQL5.7.24

    STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 
    

    MySQL5.7.25

    STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    

    MySQL5.7.26

    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
    

    由上可以得出5.7.26以及之后的版本ONLY_FULL_GROUP_BY 是官方默认的配置

  • 相关阅读:
    ASP.NET在禁用视图状态的情况下仍然使用ViewState对象【转】
    Atcoder Regular Contest 061 D Card Game for Three(组合数学)
    Solution 「CERC 2016」「洛谷 P3684」机棚障碍
    Solution 「CF 599E」Sandy and Nuts
    Solution 「洛谷 P6021」洪水
    Solution 「ARC 058C」「AT 1975」Iroha and Haiku
    Solution 「POI 2011」「洛谷 P3527」METMeteors
    Solution 「CF 1023F」Mobile Phone Network
    Solution 「SP 6779」GSS7
    Solution 「LOCAL」大括号树
  • 原文地址:https://www.cnblogs.com/zqsb/p/11003997.html
Copyright © 2011-2022 走看看