How to display number of records by category in PHP and Mysql

Asked

Viewed 87 times

0

I have a real estate table 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 make a query for each category, but I think it requires a lot of server. Is it possible to make a single query and then find a way to show the results separated by category? Ah, as there are only 6 fixed categories, I have not created another table only of categories, dispensing with the use of Join.

  • SELECT category , COUNT(category) AS totalCount FROM tableName GROUP BY category

1 answer

0


That simple SELECT uses the GROUP BY categoria to group all records with the field content categoria equal. In the columns that we carry has its own categoria and COUNT(categoria) qtde which has the function of counting all grouped lines:

SELECT categoria, COUNT(categoria) qtde FROM imoveis
GROUP BY categoria;
  • Wow, so simple. Thanks for the strength.

Browser other questions tagged

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