gpt4 book ai didi

mysql - 这个 MySQL 查询有什么问题,它有一个带有 JOIN 的子查询,应该链接来自 3 个表的数据?

转载 作者:行者123 更新时间:2023-11-30 22:32:06 25 4
gpt4 key购买 nike

我有一个用户参与的游戏,按比赛和竞赛组织,每天都有比赛,比赛每隔几年举行一次。分数基于他们预测第二天的运动成绩的接近程度。

我要执行的查询涉及三个表。一个表包含比赛元数据(如姓名和年份),另一个表按 MatchId 链接到比赛,另一个表包含每个用户和 MatchId 的每日排名。

我想得到一个结果,它给出了 MatchId 的平均分数,以及比赛元数据(通过作为中介的 Matches 表)。这三个表是:

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

关于mysql - 这个 MySQL 查询有什么问题,它有一个带有 JOIN 的子查询,应该链接来自 3 个表的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com