Error #1111 - mysql

Asked

Viewed 32 times

1

use BDCep;

select MAX(count(endereco_completo)), bairro
from tend_endereco 
inner join tend_bairro
on tend_bairro.id_bairro = tend_endereco.id_bairro
where tend_bairro.id_cidade = 109 
GROUP by bairro ORDER by COUNT(endereco_completo) desc;

#1111 - Invalid use of group Function

In summary, I have an activity to train mysql, and the question is "Which neighborhood of Maceió has the largest number of streets?" , so I tried to use the max() function, but gave this problem (error #1111). I researched a lot about this error, but nothing that guided me to the answer. The real question is, why can’t I use this function in this way and how could I answer this activity?

Note: The database has 5 tables-

endereco_completo(
  logradouro varchar(300),
  endereco varchar(300), 
  bairro varchar(200), 
  cidade varchar(200), 
  uf varchar(2), 
  cep varchar(9)
);

tend_bairro(
  id_bairro (Primária) int(11), 
  id_cidade int(11), 
  bairro (200)
);

tend_cidade(
  id_cidade (Primária) int(11),
  id_estado int(11), 
  cidade varchar(200)
);

tend_endereco(
  cep (Primária) varchar(9),
  id_cidade int(11),
  id_bairro int(11),
  logradouro varchar(300),
  endereco varchar(300),
  endereco_completo varchar(300)
); 

tend_estado(
  id_estado (Primária) int(11)
  estado varchar(150)
  uf varchar(2)
).

Obs2: they are connected by fk certinho.

1 answer

1

In mysql, you can use the LIMIT:

SELECT COUNT(endereco_completo), bairro
FROM tend_endereco 
INNER JOIN tend_bairro ON tend_bairro.id_bairro = tend_endereco.id_bairro
WHERE tend_bairro.id_cidade = 109 
GROUP BY bairro
ORDER BY COUNT(endereco_completo) DESC
LIMIT 1;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.