获取MySQL查询中每个唯一键的值的总和(Obtaining a sum of values for each unique key in MySQL query)

编程入门 行业动态 更新时间:2024-10-19 22:22:44
获取MySQL查询中每个唯一键的值的总和(Obtaining a sum of values for each unique key in MySQL query)

我有一个表,其中包含一个键列表,每个键都与一个数值相关联。 我想运行一个SQL查询来列出一列中的每个不同键以及在另一列中具有该不同键的所有值的总和。 此过程与首先选择个人相同

key | val 1 | 4 2 | 2 1 | 6 3 | 7 2 | 10

会回来:

key | sum 1 | 10 2 | 12 3 | 7

我有:

SELECT DISTINCT key as ukey, SUM(value) as sum from table

但这不起作用。 有查询吗?

I have a table with a list of keys, each associated with a numeric value. I want to run an SQL query to list each distinct keys in one column and the sum of all values that have that distinct key in another. This process is identical to first selecting individual

key | val 1 | 4 2 | 2 1 | 6 3 | 7 2 | 10

would return:

key | sum 1 | 10 2 | 12 3 | 7

I have:

SELECT DISTINCT key as ukey, SUM(value) as sum from table

But that doesn't work. Is there a query?

最满意答案

SELECT key, SUM(value) as sum FROM table GROUP BY key

这是mysql的参考:

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

SELECT key, SUM(value) as sum FROM table GROUP BY key

Here is reference from mysql:

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

更多推荐

本文发布于:2023-08-01 15:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361512.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   一键   Obtaining   MySQL   sum

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!