How to display number of records per category in PHP and MySql

0

I have a table of real estate in MySql divided into 6 categories. I want to make a query that returns the number of records for each category. The only way I know so far is to do a query for each category, but I think this requires a lot from the server. Is it possible to do a single query and then find a way to show the results sorted by category? Oh, since there are only 6 fixed categories, I did not create another category-only table, without the use of join.

    
asked by anonymous 26.10.2017 / 00:39

1 answer

0

This simple SELECT uses GROUP BY categoria to group all records with the categoria field content equal. In the columns that we have has the own categoria and COUNT(categoria) qtde that counts all the clustered lines:

SELECT categoria, COUNT(categoria) qtde FROM imoveis
GROUP BY categoria;
    
26.10.2017 / 00:52