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

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


我该怎么办?

最佳答案

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

10-05 21:26