嗨有下面的表格:teams,teamPoints,team_members,pointsTable 我正在尝试对团队成员积分求和,然后对团队积分求和:
Teams
|TeamID |TeamName | TeamLocation|
-------------------------------
|1 | sm1 | location1|
|2 | sm2 | location2|
TeamPoints
|TeamID |TotalPonts | RemainderPonts|
-------------------------------
|1 | 10 | 7 |
|1 | 8 | 6 |
|2 | 8 | 6 |
team_members
|TeamID |UserID |
-----------------
|1 | 1 |
|1 | 2 |
|2 | 3 |
pointsTable
|UserID |TotalPonts | RemainderPonts|
-------------------------------
|1 | 10 | 7 |
|2 | 8 | 6 |
|2 | 8 | 6 |
so results for team sm1 (TeamID =1) should be
TeamName=sm1,TeamPoints.TotalPonts =18, TeamPoints.RemainderPonts=13
pointsTable.TotalPonts =26, pointsTable.RemainderPonts=19
这可以总结成员积分,但团队积分有问题
SELECT
teams.TeamID AS theTeamID,
teams.TeamName,
teams.TeamLocation,
team_members.TeamID,
team_members.UserID,
SUM(pointstable.RemainderPoints) AS points
FROM
teams
LEFT JOIN
team_members ON teams.TeamID = team_members.TeamID
LEFT JOIN
pointstable ON team_members.UserID = pointstable.UserID
GROUP BY teams.TeamID
试过
SELECT
teams.TeamID AS theTeamID,
teams.TeamName,
teams.TeamLocation,
team_members.TeamID,
team_members.UserID,
SUM(pointstable.RemainderPoints) AS points,
teampoints.TeamID AS tpID,
SUM(teampoints.TotalPoints) AS teamRedeamable
FROM
teams
LEFT JOIN
team_members ON teams.TeamID = team_members.TeamID
LEFT JOIN
pointstable ON team_members.UserID = pointstable.UserID
LEFT JOIN
teampoints ON teams.TeamID = teampoints.TeamID
GROUP BY teams.TeamID
但似乎改变/乘以原始值也尝试过子选择但需要一些如何分组?
欢迎任何帮助