简体   繁体   中英

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 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...

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. ie:

SELECT category, COUNT(*) FROM table GROUP BY category

始终使用COUNT(1)加快执行速度

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM