How can i do this query in sql? count number of category
my table is:
name | category |
name01 category01
name02 category01 name03 category02 name04 category02 name05 category02 name06 category02 name07 ca开发者_开发知识库tegory02 ..... I would like to count for each category the number of namethe 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...
SELECT category, COUNT(*) FROM table GROUP BY category
Although 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 result or denormalize the table. (However, these techniques assume a really big table)
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
Always use COUNT(1) for faster execution
精彩评论