Error related to only_full_group_by when executing a query in MySql

前端 未结 18 2494
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:39

I have upgraded my system and have installed MySql 5.7.9 with php for a web application I am working on. I have a query that is dynamically created, and when run in older ve

18条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 04:51

    This is what helped me to understand the entire issue:

    1. https://stackoverflow.com/a/20074634/1066234
    2. https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

    And in the following another example of a problematic query.

    Problematic:

    SELECT COUNT(*) as attempts, SUM(elapsed) as elapsedtotal, userid, timestamp, questionid, answerid, SUM(correct) as correct, elapsed, ipaddress FROM `gameplay`
                            WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY)
                            AND cookieid = #
    

    Solved by adding this to the end:

      GROUP BY timestamp, userid, cookieid, questionid, answerid, elapsed, ipaddress
    

    Note: See the error message in PHP, it tells you where the problem lies.

    Example:

    MySQL query error 1140: In aggregated query without GROUP BY, expression #4 of SELECT list contains nonaggregated column 'db.gameplay.timestamp'; this is incompatible with sql_mode=only_full_group_by - Query: SELECT COUNT(*) as attempts, SUM(elapsed) as elapsedtotal, userid, timestamp, questionid, answerid, SUM(correct) as correct, elapsed, ipaddress FROM gameplay WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY) AND userid = 1

    In this case, expression #4 was missing in the GROUP BY.

提交回复
热议问题