MySql获取cloumn中每种类型元素的计数 [英] MySql getting count of each type of element in a cloumn
本文介绍了MySql获取cloumn中每种类型元素的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个具有以下结构的表..
I have 2 tables with the following structure..
category
{
sub_cat_id - numeric
sub_cat_name - varchar
main_cat_id - varchar (2 characters)
main_cat_name - varchar
}
products
{
pid - autoincrement
pname - varchar
category (Will be either sub_cat_id or main_cat_id)
}
产品表
pid pname category
1 mobile EL
2 shoes 1
3 shirt 1
4 TV EL
5 mp3 player EL
我想获取 products 表中每个类别的计数,以便我的查询返回2 对于类别 1 和3 为类别 EL
I want to get the count of each category in the products table such that my query returns 2 for category 1 and 3 for category EL
有人可以帮我吗??
推荐答案
这应该是您想要的查询:
This should be the query you're after:
SELECT category, COUNT(*) FROM products GROUP BY category;
我建议您考虑一下类别表的设计,因为这可能会给您带来麻烦.
I would recommend thinking about the design of your category table, as it might cause you difficulties in the future.
这篇关于MySql获取cloumn中每种类型元素的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文