[英]How can i do this query in sql? count number of category
my table is: 我的桌子是:
name01 category01 名称01类别01
name02 category01 名称02类别01
name03 category02 名称03类别02
name04 category02 名称04类别02
name05 category02 名称05类别02
name06 category02 名称06类别02
name07 category02 名称07类别02
..... 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): 我想要的输出是这样的(通过上面的示例):
category01 2 类别01 2
category02 5 类别02 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. 这是非常基本的,但是使用GROUP BY语句应该可以得到期望的结果。 ie:
即:
SELECT category, COUNT(*) FROM table GROUP BY category
始终使用COUNT(1)加快执行速度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.