Aggregating Queries without GROUP BY in MySQL
Your query is encountering a syntax error when aggregating data without specifying a GROUP BY clause. This behavior was introduced in MySQL 5.7.5 as part of an effort to ensure the integrity of data aggregation.
The error message explains that non-aggregated columns, such as id in your query, cannot appear in the SELECT list when an aggregate function (such as COUNT()) is used without a GROUP BY clause. This ensures that the results of the aggregation are accurate and consistent.
Resolving the Error
You have two options for resolving this error:
SELECT id, password, COUNT(id) AS count FROM users WHERE email = :email GROUP BY id, password LIMIT 1
Alternatively, if you can ensure that the non-aggregated column (e.g., id) is limited to a single value in the WHERE clause, you can exclude it from the GROUP BY clause. Refer to the provided link for examples of this exception.
By using one of these methods, you can resolve the error and ensure the correct aggregation of data in your MySQL query.
The above is the detailed content of Why Can\'t I Aggregate Queries in MySQL Without a GROUP BY Clause?. For more information, please follow other related articles on the PHP Chinese website!