1

我有下表:

order_id  product_id
1         102
2         105
3         102
4         96
5         96

我将如何获得product_ids 的计数。我希望达到的结果是:

product_id    count
96            2
102           2
105           1

我试过的查询是:

SELECT product_id, sum(product_id) from orders

但这当然会产生只有一行的错误结果。这里正确的查询是什么?

4

2 回答 2

3
SELECT product_id, COUNT(*) FROM orders GROUP BY product_id

或者如果你想命名它...

SELECT product_id, COUNT(*) as product_count FROM orders GROUP BY product_id
于 2013-08-02T00:00:24.883 回答
0

尝试这个

 SELECT product_id, count(*) as count  from orders GROUP BY product_id
于 2013-08-02T00:00:28.143 回答