I can not perform the query that returns me the number of municipalities of the reports, example:
|municipio | cantidad |
|----------|----------|
|ACAJETE | 4 |
|PANTEPEC | 5 |
|PUEBLA | 1 |
In the report table: table reports
idReporte
descripcion
localidad_id fk
Table locations:
idLocalidad
nombreLocalidad
municipio_id fk
Table municipalities
idMunicipio
nombreMunicipio
region_id fk
Regions table:
idRegion
nombreRegion
To carry out the consultation I need I have related 4 tables which were mentioned above, but I can not get the amount of municipalities that were made in the reports. What I have is the following:
SELECT m.nombre, count(m.idMunicipio) as cantidad_municipios
FROM reportes
left join localidades ON reportes.localidad_id = localidades.idLocalidad
left join municipios m on m.idMunicipio = localidades.Municipio_id
left join regiones on regiones.idRegion = m.region_id WHERE regiones.idRegion=4;
It returns this result to me but it is not what I want and I do not understand how to make the query
nombre cantidad_municipios
ACAJETE 10
The result I want to obtain for the report is as follows:
|municipios | cantidad_municipios |
|-----------|---------------------|
|ACAJETE | 4 |
|PANTEPEC | 5 |
|PUEBLA | 1 |
With these results I will make a bar graph that shows an example like: In acajete 4 reports were obtained, pantatepec: 5 reports and so on