您好我有一张如下表格,我需要为给定的不同订单ID选择不同商品ID的数量我无法以MySQL格式执行此操作
+-----------+---------+
| OrderID |TradeID |
+===========+=========+
| 1 | 58761 |
+-----------+---------+
| 1 | 58762 |
+-----------+---------+
| 2 | 58763 |
+-----------+---------+
| 2 | 58764 |
+-----------+---------+
| 2 | 58765 |
+-----------+---------+
需要结果为
+-----------+---------+
| OrderID |Count |
+===========+=========+
| 1 | 2 |
+-----------+---------+
| 2 | 4 |
+-----------+---------+
答案 0 :(得分:0)
只需COUNT / GROUP BY
SELECT OrderID, COUNT(*)
FROM some_table
GROUP BY OrderID
SQL小提琴: -
http://www.sqlfiddle.com/#!9/305630/1
请注意,如果您想为每个OrderID使用几个不同的TradeID,则可以使用
SELECT OrderID, COUNT(DISTINCT TradeID)
FROM some_table
GROUP BY OrderID