指定的字段可以引用我的SQL语句的FROM子句中列出的多个表

时间:2014-03-12 11:49:37

标签: mysql

指定的字段为MatchID。无法弄清楚如何解决它。我尝试在tblPlayerStatistics之后将MatchID添加到GROUP BY,但是没有效果。

SELECT tblPlayerStatistics.Player_ID_Number, 
SUM(tblPlayerStatistics.TwoPointerMade + tblPlayerStatistics.ThreePointerMade)
AS points, MatchID
FROM tblPlayerStatistics INNER JOIN tblGameResults 
     ON tblPlayerStatistics.MatchID = tblGameResults.MatchID
GROUP BY tblPlayerStatistics.Player_ID_Number,MatchID;

3 个答案:

答案 0 :(得分:0)

尝试

SELECT tblPlayerStatistics.Player_ID_Number, 
SUM(tblPlayerStatistics.TwoPointerMade + tblPlayerStatistics.ThreePointerMade) AS points,    
tblPlayerStatistics.MatchID 
FROM tblPlayerStatistics 
INNER JOIN tblGameResults ON tblPlayerStatistics.MatchID = tblGameResults.MatchID 
GROUP BY tblPlayerStatistics.Player_ID_Number, tblPlayerStatistics.MatchID;

您需要在GROUP BY

中指定MatchID所属的表格

答案 1 :(得分:0)

您还可以使用using子句解决此问题:

SELECT ps.Player_ID_Number, 
       SUM(ps.TwoPointerMade + ps.ThreePointerMade) AS points, 
       MatchID
FROM tblPlayerStatistics ps INNER JOIN
     tblGameResults gr
     USING (MatchId)
GROUP BY ps.Player_ID_Number, MatchID;

我还引入了表别名(缩写)以使查询更具可读性。

答案 2 :(得分:0)

您的查询应如下所示

SELECT tps.Player_ID_Number, 
SUM(tps.TwoPointerMade + tps.ThreePointerMade) AS points,
tps.MatchID                        <-- Here refer the table where MatchID taken
FROM tblPlayerStatistics tps INNER JOIN tblGameResults tgr
     ON tps.MatchID = tgr.MatchID
GROUP BY tps.Player_ID_Number,tps.MatchID;  <--So does Here