MYSQL在按不同行分组时行的Sum字段值

时间:2014-04-11 05:53:54

标签: mysql sum

我需要一点提升,所以任何帮助之手都会受到赞赏。 这是一个示例表:

 ----------------------------------------------------
|Order # |   Name    |    product   |    Quantity    |
 ----------------------------------------------------
| 15     |   John    |   Scooter    |        1       |
 ----------------------------------------------------
| 15     |   John    |   Scooter    |        1       |
 ---------------------------------------------------- 
| 15     |   John    |   Bicycle    |        1       |
 ---------------------------------------------------- 
| 16     |   Jane    |   Wheel      |        1       |
 ---------------------------------------------------- 
| 16     |   Jane    |   Pedal      |        1       |
 ---------------------------------------------------- 
| 16     |   Jane    |   Pedal      |        1       |
 ---------------------------------------------------- 
| 17     |   Mike    |   Scooter    |        1       |
 ---------------------------------------------------- 

我正在寻找的输出是:

 ---------------------------------------------------- 
| 15     |   John    |   Scooter    |        2       |
 ---------------------------------------------------- 
| 15     |   John    |   Bicycle    |        1       |
 ---------------------------------------------------- 
| 16     |   Jane    |   Wheel      |        1       |
 ---------------------------------------------------- 
| 16     |   Jane    |   Pedal      |        2       |
 ---------------------------------------------------- 
| 17     |   Mike    |   Scooter    |        1       |
 ---------------------------------------------------- 

我如何在MYSQL中解决这个问题?

5 个答案:

答案 0 :(得分:0)

尝试使用:

select `order #`,name,product,sum(quantity) from your_table group by `order #`,name,product

答案 1 :(得分:0)

select 
  `Order #`, Name, product, sum(Quantity) total
from 
  table_name
group by
  1,2,3

答案 2 :(得分:0)

试试这个:

select `Order`, Name, product, sum(Quantity) as total
from tablename
group by
product,name

答案 3 :(得分:0)

试试这个

SELECT Order,Name,product,sum(Quantity) as Quantity  
FROM table_name GROUP BY product,name

答案 4 :(得分:0)

您需要使用group by

   Select `order #`, name, product, sum(quantity)
   from table_name
   group by `order #`, name, product

fiddle