views:

131

answers:

4

my table is:

name | category |

name01 category01
name02 category01
name03 category02
name04 category02
name05 category02
name06 category02
name07 category02
..... I would like to count for each category the number of name

the output i woulk like is something like this (by the example above):

category | num

category01 2
category02 5
...

thanks to all that would to help me...

+1  A: 
SELECT category, COUNT(*) FROM table GROUP BY category

Altough count could be a bit slow on very big tables, so if you run a very big service or operate on a lot of data you might consider cache the resoult or denormalize the table. (However, it these techiques need a really big table)

erenon
You posted first... I deleted my post.
Cesar
@Cesar: I really appreciate it, thanks.
erenon
+1  A: 

this is very basic, but using a GROUP BY statement should give the desired result. i.e:

SELECT category, COUNT(*) FROM table GROUP BY category
Salandur
A: 

Always use COUNT(1) for faster execution

Turek
A: 

Here is a hypothetical question.

Say that category was indexed. Would it be any faster to write the query like this

select category, count(category) from table group by category

Brian