我有两个表,需要得到两列的总和(每个表中有一列)。两个表中两列的总和
- 桌上游戏(id_tournament,id_game,电子邮件,点)PK(id_tournament,id_game,电子邮件)
- 表中的结果(id_tournament,电子邮件,点)PK(id_tournament,电子邮件)
- 表用户(id_user ,电子邮件,姓名)PK(id_user)
示例表结果:
1,[email protected],3
1,[email protected],4
示例表游戏:
1,1,[email protected],5
1,2,[email protected],3
1,3,[email protected],2
1,1,[email protected],4
1,3,[email protected],2
示例表用户:
1,[email protected],John
2,[email protected],Peter
我的输出应该是:
John 13
Peter 10
我试图用
SELECT t3.name, SUM(t1.points+t2.points) as Total
FROM user t3, games t1, results t2
WHERE t1.email=t2.email AND t2.email=t3.email AND t1.id_tournament=t2.tournament
GROUP BY t1.email
ORDER BY Total DESC
却得到了非常奇怪的数字。
如果你想看到你的'奇异数'来自,只是删除组和总和。 (可以显示列,你可以为特定用户完成此操作,并自己进行数学计算。)您将看到您的联接导致问题;这可以通过斯宾塞的答案来解决。 –