Home > Database > Mysql Tutorial > body text

How to resolve \'Invalid use of group function\' error in MySQL when finding maximum record count?

Mary-Kate Olsen
Release: 2024-10-25 03:35:02
Original
863 people have browsed it

How to resolve

Finding Maximum Record Count in MySQL

In MySQL, when attempting to find the maximum record count using the max(count(*)) aggregation function, you may encounter the error "Invalid use of group function." To resolve this issue, it's necessary to modify the query to correctly determine the maximum count.

In the provided query:

select max(count(*)) from emp1 group by name;
Copy after login

The group function count(*) is used within the max function, resulting in the error. To fix this, you can modify the query as follows:

SELECT NAME, 
       COUNT(*) as c 
FROM table 
GROUP BY name 
ORDER BY c DESC LIMIT 1
Copy after login

This query will first calculate the count of records for each unique value in the name column and assign the count to a new column named c. It then groups the results by the name column, sorts them in descending order based on the c column, and retrieves only the first row, which contains the maximum count for any unique name.

The above is the detailed content of How to resolve \'Invalid use of group function\' error in MySQL when finding maximum record count?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!