这个 MySQL 查询有什么问题,它有一个带有 JOIN 的子查询,应该是来自 3 个表的 link 数据?
What is wrong with this MySQL query, that has a sub-query with a JOIN, that is supposed to link data from 3 tables?
我有一个用户参与的游戏,按比赛和比赛组织,每天都有比赛,比赛每隔几年举行一次。分数基于他们预测第二天的运动成绩的接近程度。
我要执行的查询涉及三个 table。一个 table 包含比赛元数据(如姓名和年份),另一个链接匹配到比赛的 MatchId,另一个包含每个用户和 MatchId 的每日排名。
我想得到一个结果,它给出了 MatchId 的平均分数,以及比赛元数据(通过 Matches table 作为中介)。三个 table 是:
DailyStandings:
-UserId (int)
-MatchId (int)
-MatchScore (double)
-"UserId, MatchId" is the primary key
Matches:
-MatchId (int, primary key)
-ContestId (int)
Contests:
-ContestId(int, primary key)
-Name ( varchar(50) )
-Year (smallint)
我正在尝试以下查询:
SELECT DailyStandings.MatchId, AVG(DailyStandings.MatchScore) AS Average, ContestInfo.Name
FROM DailyStandings
INNER JOIN{
SELECT * FROM Matches
LEFT JOIN Contests
ON Matches.ContestId=Contests.ContestId
} ContestInfo
ON DailyStandings.MatchId=ContestInfo.MatchId
WHERE 1
GROUP BY DailyStandings.MatchId ORDER BY DailyStandings.MatchId ASC
请注意,我不确定我是否应该在此处使用 INNER JOIN 或 LEFT JOIN,但似乎都不起作用。
INNER JOIN 中的子查询工作正常,如果我去掉子查询(以及 SELECT 中的 ContestInfo.Name),那么主查询工作正常。
也许我犯了一个明显的错误,但我的错误是什么?
试试这个:
SELECT DailyStandings.MatchId, AVG(DailyStandings.MatchScore) AS Average,
Matches.MatchId, Matches.ContestId
FROM DailyStandings
INNER JOIN Matches
ON DailyStandings.MatchId=Matches.MatchId
Inner Join Contests
ON Contests.ContestId=Matches.ContestId
WHERE 1 /* Not sure what is "1". Try removing this first and then execute the query. */
GROUP BY DailyStandings.MatchId ORDER BY DailyStandings.MatchId ASC
我有一个用户参与的游戏,按比赛和比赛组织,每天都有比赛,比赛每隔几年举行一次。分数基于他们预测第二天的运动成绩的接近程度。
我要执行的查询涉及三个 table。一个 table 包含比赛元数据(如姓名和年份),另一个链接匹配到比赛的 MatchId,另一个包含每个用户和 MatchId 的每日排名。
我想得到一个结果,它给出了 MatchId 的平均分数,以及比赛元数据(通过 Matches table 作为中介)。三个 table 是:
DailyStandings:
-UserId (int)
-MatchId (int)
-MatchScore (double)
-"UserId, MatchId" is the primary key
Matches:
-MatchId (int, primary key)
-ContestId (int)
Contests:
-ContestId(int, primary key)
-Name ( varchar(50) )
-Year (smallint)
我正在尝试以下查询:
SELECT DailyStandings.MatchId, AVG(DailyStandings.MatchScore) AS Average, ContestInfo.Name
FROM DailyStandings
INNER JOIN{
SELECT * FROM Matches
LEFT JOIN Contests
ON Matches.ContestId=Contests.ContestId
} ContestInfo
ON DailyStandings.MatchId=ContestInfo.MatchId
WHERE 1
GROUP BY DailyStandings.MatchId ORDER BY DailyStandings.MatchId ASC
请注意,我不确定我是否应该在此处使用 INNER JOIN 或 LEFT JOIN,但似乎都不起作用。
INNER JOIN 中的子查询工作正常,如果我去掉子查询(以及 SELECT 中的 ContestInfo.Name),那么主查询工作正常。
也许我犯了一个明显的错误,但我的错误是什么?
试试这个:
SELECT DailyStandings.MatchId, AVG(DailyStandings.MatchScore) AS Average,
Matches.MatchId, Matches.ContestId
FROM DailyStandings
INNER JOIN Matches
ON DailyStandings.MatchId=Matches.MatchId
Inner Join Contests
ON Contests.ContestId=Matches.ContestId
WHERE 1 /* Not sure what is "1". Try removing this first and then execute the query. */
GROUP BY DailyStandings.MatchId ORDER BY DailyStandings.MatchId ASC