SQL查询 - 选择用户名并对其行求和

时间:2013-12-16 22:33:01

标签: sql

我有这个查询

SELECT username FROM paymentValue

我可以将paymentValue加上像

这样的用户名
SELECT username, SUM(paymentValue) WHERE username = 'john'

但是如何为所有用户名选择总和以获得返回: 约翰,146 结婚,3456 安娜,2043年

3 个答案:

答案 0 :(得分:3)

使用'group by'子句

select username, sum(paymentvalue) as sumofpayments from thetable
group by username

答案 1 :(得分:1)

SELECT username, SUM(paymentValue) 
FROM TableName
GROUP BY username

与另一张桌子加入

SELECT username, SUM(paymentValue) 
FROM TableName INNER JOIN SecondTable
ON TableName.RefrencingColumn = SecondTable.RefrencingColumn  
WHERE username = 'john'
GROUP BY username

答案 2 :(得分:0)

select username,
       sum(paymentValue) as Payments
from paymentValue
group by username