如何获得另一列中每个不同值的总和? [英] How get the sum for every distinct value in another column?

查看:84
本文介绍了如何获得另一列中每个不同值的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于如何获得另一列中每个不同值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆