开发者

How can i do this query in sql? count number of category

开发者 https://www.devze.com 2022-12-12 00:00 出处:网络
my table is: name | category | name01 category01 name02 category01 name03 category02 name04 category02 name05 category02

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 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. i.e:

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


Always use COUNT(1) for faster execution

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号