[英]Get the sum of the column in mysql
Below is the table I have created, my goal is to get the sum of the value depending of each person has, and exclude any username duplicate. 下面是我创建的表格,我的目标是获取每个人所拥有的价值总和,并排除任何用户名副本。
username | value
Bob 5
Vicky 10
Bob 12
Desire results: 欲望结果:
username | value
Bob 17
Vicky 10
This is what the GROUP BY
clause do, use GROUP BY username
with SUM(value)
: 这是GROUP BY
子句的作用,使用GROUP BY username
和SUM(value)
:
SELECT username, SUM(value)
FROM tablename
GROUP BY username;
When you add a GROUP BY
clause, rows that have the same values in the list of columns you specify in it, will be gathered into a group of unique values. 添加GROUP BY
子句时,在其中指定的列列表中具有相同值的行将收集到一组唯一值中。 For the other columns that are not listed in the GROUP BY
clause, the aggregate function will be applied to it, the SUM(value)
in the query above. 对于未在GROUP BY
子句中列出的其他列,将对其应用聚合函数,即上面查询中的SUM(value)
。
Try this 尝试这个
SELECT T.username, SUM(T.value) AS value
FROM your_table_name T
WHERE T.username IS NOT NULL
GROUP BY T.username
试试这个
SELECT username, SUM(value) as val FROM tablename GROUP BY username;
Use the following query: 使用以下查询:
SELECT url
, SUM(pos) FROM import_images
GROUP BY url
SELECT url
,SUM(pos)FROM import_images
GROUP BY url
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.