我在mysql中有一个表,我想要这个输出:
HNSHAK MOBILE COMPUTING =13
HNSHAK STORAGE =2
每次执行时,产品组都会更改。
表:
Productgroup Created By
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
STORAGE HNSHAK
MOBILE COMPUTING HNSHAK
STORAGE HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
MOBILE COMPUTING HNSHAK
答案 0 :(得分:1)
您的查询应该是这样的:
SELECT `Created By`, ProductGroup, COUNT(*) `Count`
FROM tablename
GROUP BY `Created By`, ProductGroup;
使用GROUP BY Created_By, ProductGroup
和汇总函数COUNT
。
这会给你:
Created By ProductGroup Count
HNSHAK MOBILE COMPUTING 13
HNSHAK STORAGE 2
答案 1 :(得分:0)
SELECT `Created_By`, `Productgroup`, COUNT(`Productgroup`)
FROM `table_name`
GROUP BY `Productgroup`
您应该在这里使用GROUP BY
。您还应该记住,如果Created By
中的所有值都不相同(例如,其中一个值是COMPANY1
),那么您要忽略Productgroup
并选择表格中显示的Productgroup
的第一个值,或者每个_manufacturer的产品组都有多行。要实现后者,您将以GROUP BY
这种方式使用
GROUP BY `Created_By`, `Productgroup`