如何获得另一列中每个不同值的总和?

问题描述:

假设我的表格包含以下内容:

Suppose my table contains the following:

ID    price    
-------------
 1     10      
 1     10      
 2     20      
 2     20      
 3     30
 3     30

每次通过while循环。我想得到价格的价值的总和,但是ID中的每个不同的值只有一次

Each time through a while loop. I want to get the sum of the values on price but only once for every distinct value in ID

所以给出上面的例子,我会得到类似: p>

So given the example above, I would arrive with something like:

ID    price    
-------------
 1     20          
 2     40           
 3     60

只是为了补充,类似的查询。问题是,其他帖子使用Oracle,我另一方面使用MySQL。

Just to add, I may have stumble upon a post with a similar inquiry. Problem is that other post uses Oracle, I on the other hand am using MySQL.

您需要 GROUP BY SUM

SELECT ID, 
       SUM(price) AS price
FROM YourTable
GROUP BY ID