美文网首页
sql_mode=only_full_group_by错误

sql_mode=only_full_group_by错误

作者: 王侦 | 来源:发表于2019-11-25 20:42 被阅读0次

    1.错误现象

       <insert id="insertProductSellDaily" parameterType="com.imooc.o2o.entity.ProductSellDaily">
            INSERT
            INTO
            tb_product_sell_daily(product_id,shop_id,create_time,total)
            (
            SELECT
            product_id,shop_id,date_format(create_time,'%Y-%m-%d'),count(product_id) AS total FROM
            tb_user_product_map
            WHERE
            date_format(create_time,'%Y-%m-%d') = date_sub(curdate(),interval 1 day)
            GROUP BY product_id)
        </insert>
    

    语句:

    INSERT INTO tb_product_sell_daily(product_id,shop_id,create_time,total)
     ( SELECT product_id,shop_id,date_format(create_time,'%Y-%m-%d'),count(product_id) AS total 
    FROM tb_user_product_map 
    WHERE date_format(create_time,'%Y-%m-%d') = date_sub(curdate(),interval 1 day) 
    GROUP BY product_id) 
    

    SELECT list is not in GROUP BY clause and contains nonaggregated column 'o2o.tb_user_product_map.shop_id' which is not functionally
    dependent on columns in GROUP BY clause; this is incompatible with
    sql_mode=only_full_group_by

    2.错误原因

    MySQL 5.7.5及以上功能依赖检测功能。如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列表的查询引用在GROUP BY子句中既未命名的非集合列,也不在功能上依赖于它们。(5.7.5之前,MySQL没有检测到功能依赖关系,默认情况下不启用ONLY_FULL_GROUP_BY。有关5.7.5之前的行为的说明,请参见“MySQL 5.6参考手册”。)以下解决方案并不是最终解决方案,只是屏蔽了报错,但问题依然存在。

    3.临时性解决方法

    mysql> select @@global.sql_mode;
    +-----------------------------------------------------------------------------------------------------------------------+
    | @@global.sql_mode                                                                                                     |
    +-----------------------------------------------------------------------------------------------------------------------+
    | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
    +-----------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    

    在/etc/my.cnf中设置(去掉ONLY_FULL_GROUP_BY)

    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
    

    然后重启数据库

    相关文章

      网友评论

          本文标题:sql_mode=only_full_group_by错误

          本文链接:https://www.haomeiwen.com/subject/sgtswctx.html