How to Sum column using group by in mysql?

时间:2016-12-09 12:45:42

标签: mysql

------------------------------------
Item         |     Box
------------------------------------
10           |      25
------------------------------------
12           |      50
------------------------------------
3            |      25
------------------------------------
4            |      30
------------------------------------
6            |      50
------------------------------------

Output Should be

[25] => 13   , [50] => 18, [30] => 4

I am using following query but i am getting the result

  select sum(item) as qt  from table group by box

Please help me to solve my problem

1 个答案:

答案 0 :(得分:2)

try this :

SELECT sum(item) as qt, box
FROM tablename
GROUP BY box
ORDER BY qt