Sql 查询问题-结果不正确

Sql 查询问题-结果不正确,sql,mysql,Sql,Mysql,我很难解决这个问题。请帮忙 我有一个名为Product_Information的表。我想计算类别和子类别中存在的产品数量 这是桌子 Product_Id - Product_Title - Product_Sub_Category - Product_Category 1 ----------------abc------------------XYX------------------X 2 ----------------def------------------XYX----------

我很难解决这个问题。请帮忙

我有一个名为Product_Information的表。我想计算类别和子类别中存在的产品数量

这是桌子

Product_Id - Product_Title - Product_Sub_Category - Product_Category 
1 ----------------abc------------------XYX------------------X
2 ----------------def------------------XYX------------------Z
3 ----------------ghi------------------XYX------------------X
4 ----------------jkl------------------XYM------------------Z
我希望结果是这样的

result 
------

Product_Category-Product_Sub_Category-count(Product_Id) 
X--------------------XYX-------------------------2
Z--------------------XYX-------------------------1
Z--------------------XYM-------------------------1
(很抱歉以错误的方式提供信息)

我使用了以下查询:

Select
Product_Category,
Product_Sub_Category,
count(`Product_Id`)
from product_information 
group by 
Product_Category

但这给了我错误的结果。

如果您只需要特定子类别中的产品数量,请使用:

select count(*) from Product_Information 
where Product_Category = ? and Product_Sub_Category = ?
如果您需要所有的数字,那么您需要这样分组:

select Product_Category, Product_Sub_Category, count(*) 
from Product_Information 
group by Product_Category, Product_Sub_Category;

您可以使用分析函数和分区方式,也可以单独执行两个查询(如果愿意,可以将它们组合到一个大型查询中),但以下是基本查询: 按类别计数

Product_Category, count(Product_Id)

from product_information

group by Product_Category
Product_Category, Product_Sub_Category, count(Product_Id)

from product_information

group by Product_Category, Product_Sub_Category
按子类别计数

Product_Category, count(Product_Id)

from product_information

group by Product_Category
Product_Category, Product_Sub_Category, count(Product_Id)

from product_information

group by Product_Category, Product_Sub_Category

更改查询:选择

Product_Category, Product_Sub_Category, count(Product_Id) from product_information group by Product_Category , Product_Sub_Category ; 产品类别、产品子类别、计数(产品Id) 从产品信息 按产品类别、产品子类别分组;
这会给你很好的结果

你能编辑你的问题并更好地表达它吗?具体来说,你能把你的表和查询放在一个代码块中吗?你不需要
按产品类别分组,产品类别分组
,你只需要
按产品类别分组
,在你问题的代码中问题是,如果我使用查询2,我会得到一个混合的结果。意思是我看到了一个类别中不存在的子类别。。好心帮忙