我刚刚开始学习SQL,并遇到了绊脚石.我有一些看起来像这样的代码:
SELECT player, SUM(wins) from ( SELECT win_counts.player, win_counts.wins from win_counts UNION SELECT lose_counts.player, lose_counts.loses from lose_counts group by win_counts.player ) as temp_alias
这是我得到的错误:
错误:缺少表"win_counts"的FROM-clause条目第7行:按win_counts.player分组
此win_counts表包含玩家ID列表以及他们拥有的匹配数.lose_counts表包含玩家ID列表以及丢失的匹配数.最终我需要一个玩家ID表和每个玩家所玩的比赛总数.
感谢您的帮助.对不起,我没有更多的信息...我对sql的理解非常简陋.
分组似乎在错误的地方.
SELECT player, SUM(wins) as SumWinsLoses FROM( SELECT win_counts.player, win_counts.wins FROM win_counts UNION ALL -- as Gordon points out 'ALL' is likely needed, otherwise your sum will be -- off as the UNION alone will distinct the values before the sum -- and if you have a player with the same wins and losses (2), -- the sum will return only 2 instead of (4). SELECT lose_counts.player, lose_counts.loses FROM lose_counts) as temp_alias GROUP BY player
我们很清楚,虽然SUm(Wins)将总和输赢,因为"胜利"字段联盟中的第一个名称是使用的名称.因此,球员的胜负将被汇总.
这是一个有效的SQL FIddle 通知,没有联合所有......玩家#2的计数不正确.