September 28, 2008

How do I get the number of rows of each type with MySQL?

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?

Tags: , ,

Filed under Programming & Design by administrator

Permalink Print

Comments on How do I get the number of rows of each type with MySQL?

September 30, 2008

marsulein @ 10:36 am

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);

October 1, 2008

dartarrow @ 6:40 am

SELECT type, count(type) FROM my_table_name GROUP BY type