MYSQL - 来自三个表的SUM点

时间:2013-04-09 23:51:49

标签: mysql sum inner-join

我有以下SQL语句(到目前为止):

SELECT p.facebookID,p.points,pi.facebookID,pi.points, SUM(pi.points) FROM player AS p 
INNER JOIN invites AS i ON p.id = i.inviteFromID 
INNER JOIN player AS pi ON i.inviteToId  = pi.facebookID
WHERE p.id = 1 GROUP BY pi.points    

这将告诉我,UserId邀请哪个用户= 1以及他们获得了哪些点。

目标:
我需要选择UserId的所有邀请点的SUM 我怎样才能做到这一点?我的方式是正确的吗?

1 个答案:

答案 0 :(得分:2)

你不应该按照pi.points分组,而是按用户ID分组,我相信......试试这个

SELECT p.facebookID,p.points,pi.facebookID,pi.points, SUM(pi.points) FROM player AS p 
INNER JOIN invites AS i ON p.id = i.inviteFromID 
INNER JOIN player AS pi ON i.inviteToId  = pi.facebookID
WHERE p.id = 1 GROUP BY p.id

假设p.id是您的用户ID

让我知道这是如何运作的