How do I get the number of rows of each type with MySQL?
Anonymous asked:
I have a database table with two fields: id (an unique numerical identifier) and type (a string). I would like to count the number of occurrences of each of the possible values of ‘type’. Is there an easy way of doing this with MySQL?
Comments on How do I get the number of rows of each type with MySQL?
I think your best bet is to create a view to select all the values of “type” since you do not want them repeated.
SELECT DISTINCT type FROM type_table;
Rename the view as VW_TYPE.
SELECT Count(id) AS TOTAL
WHERE type = (SELECT * FROM VW_TYPE);
SELECT type, count(type) FROM my_table_name GROUP BY type