MySQL依靠group by计数多列

时间:2014-01-02 09:30:40

标签: mysql

我有一个包含以下列的数据库:

id (untique id)
product_matrix (Letter between A-Z to base priicing structre on)
sold (qty of that item sold)

如果我运行这个:

SELECT `product_matrix` as matrix, count(sold) as qty_sold FROM `products` WHERE `sold` > 0 group by `product_matrix`

我按产品矩阵分组销售数量。

我想得到的是:

矩阵(矩阵字母) qty_sold(计算按矩阵字母分组的项目) 产品(在该矩阵内销售的产品数量)

Matrix    sold    products
A         12      2
B         6       6
C         1       1

我该怎么做?

1 个答案:

答案 0 :(得分:1)

SELECT `product_matrix` as matrix, 
        sum(sold) as qty_sold,
        count(id) as products
FROM `products` 
WHERE `sold` > 0 
group by `product_matrix`