Aggregated query without GROUP BY

前端 未结 4 2048
慢半拍i
慢半拍i 2020-12-10 11:28

This query seems to work perfect on my older machine. However, on my new machine with MySQL 5.7.14 and PHP 5.6.25 it seems to throw an error:

Fatal er

相关标签:
4条回答
  • 2020-12-10 11:52

    A change was made in version 5.7-ish where it will now, by default, reject queries in which you aggregate using a function (sum, avg, max, etc.) in the SELECT clause and fail to put the non-aggregated fields in the GROUP BY clause. This behavior is part and parcel to every other RDBMS and MySQL is finally jumping on board.

    You have two options:

    1. You can change the MySQL settings to default to the old behavior to allow not-so-great queries like this. Information can be found here
    2. You can fix your query

    Option 2 would look something like:

    SELECT id, password, COUNT(id) AS count FROM users WHERE email = :email GROUP BY id, password LIMIT 1
    
    0 讨论(0)
  • 2020-12-10 11:58

    Change ur SQL mode to default.. it will execute without error The SQL mode defines the syntax of the query. If you using ONLY_FULLY_GROUPBY you have to write a query with group by for all aggregator functions

    0 讨论(0)
  • 2020-12-10 12:01

    Its a little late but I just ran into this error.

    This command might be useful for anyone else who runs into the same error

         mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
    

    More information about this can be found at Table Plus and other links quoted above by JNevill.

    Hope it helps someone else.

    0 讨论(0)
  • 2020-12-10 12:02

    Easiest answer, in config/database.php make sure to set strict => true to strict => false for the mysql settings.

    This will allow for less than strict queries at a cost of security for normal well-formed sql calls (still not 100% secure), but will allow for the use of other sql calls that could be in-secure if written improperly.

    0 讨论(0)
提交回复
热议问题