php - invalid use of group

374

i've been trying to do this on php, reading from a db table in mysql, but i cant understand what's the error on it. can someon help me please? the table as a column that contains numbers (this numbers represent a course :P)

SELECT *, MAX(COUNT(mi_curso)) maximo
FROM producto
GROUP BY mi_curso

and this is the error shown:

consulta SQL:
SELECT *, MAX(COUNT(mi_curso)) maximo
FROM producto
GROUP BY mi_curso LIMIT 0, 25

MySQL ha dicho: Documentación 
#1111 - Invalid use of group function
3

Answer

Solution:

You are trying to nest two aggregate functions, which is not working. Instead, use one of the following two queries:

SELECT *, MAX(mi_curso) maximo
FROM producto
GROUP BY mi_curso

or

SELECT *, COUNT(mi_curso) maximo
FROM producto
GROUP BY mi_curso
37

Answer

Solution:

could you try the following and comment if this worked?

SELECT id, COUNT(mi_curso) maximo FROM producto GROUP BY mi_curso having max(count(mi_curso))

edit: as suggested by Tim Biegeleisen

SELECT id, COUNT(mi_curso) maximo FROM producto GROUP BY mi_curso having max(count(maximo))
452

Answer

Solution:

How about probably the most common problem with group by....

Including non-aggregates in the select list....

select * probably contains some column other than mi_curso. If so, this group by doesn't work.

You can select aggregates, and columns included in the GROUP BY expression.

If you do (for example)

select name, address, count(name)
from residences
group by name

What value should sql deliver for address when one person has >1 residences? No way for it to know.

Some sql's give nice clear error messages. Some just wink at you, as if you already know what's wrong.

People are also looking for solutions to the problem: php - How to use Post method without a Form

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.