MySQL Couting副本重复

时间:2017-06-12 01:33:42

标签: mysql sql

我有一个充满订单的数据库,其中包含idcustomerName。我想要做的是,找出有多少不同的订单尺寸。如果客户发出多个订单,则会有多行包含相同的customerName

我想要一个看起来像这样的输出:

+------------+----------+
| size       | quantity |
+------------+----------+
| 1          |      321 |
| 2          |      148 |
| 3          |       98 |
| 4          |       63 |
| 5          |       22 |
| 6          |        3 |
+------------+----------+

编辑1:

+------------------+-----------------+------+-----+---------+----------------+
| Field            | Type            | Null | Key | Default | Extra          |
+------------------+-----------------+------+-----+---------+----------------+
| id               | int(6) unsigned | NO   | PRI | NULL    | auto_increment |
| signupDate       | date            | YES  |     | NULL    |                |
| customerName     | varchar(128)    | YES  |     | NULL    |                |
+------------------+-----------------+------+-----+---------+----------------+

2 个答案:

答案 0 :(得分:3)

select 
size,count(1)
from
(
select customerName,count(1) as size from table group by customerName
) tmp group by  size

如果这是你想要的?

答案 1 :(得分:0)

我假设您有另一张显示尺寸和数量的订单表

ID,OrderNo,Size,Qty

在这种情况下,此查询可能会有所帮助

select Size, SUM(Qty) as Qty 
From Orders
GROUP BY Size