gpt4 book ai didi

MYSQL查询显示客队名称和主队名称

转载 作者:行者123 更新时间:2023-11-29 09:48:19 24 4
gpt4 key购买 nike

我有两张 table 。
游戏包括 Date、hteam、ateam、hscore、ascore。
team 包括 id、tname。

此查询:

SELECT
games.DATE,
teams.tname AS "Away",
games.ascore AS "A Score",
games.hteam AS "Home",
games.hscore AS "H Score"
FROM
games
INNER JOIN teams ON games.ateam = teams.id
LEFT OUTER JOIN teams AS t
ON
games.hteam = t.tname
ORDER BY
games.DATE ASC

最终产生以下输出:

DATE Ascending 1        Away        A Score Home    H Score     
2008-01-01 20:00:00 Saxons 2 3 0
2008-01-01 20:00:00 Spurs 0 1 1
2008-01-08 20:00:00 Saxons 1 2 2
2008-01-08 20:00:00 Celtics 1 1 1

我的问题是如何让主队显示为其名称,而不是其 ID,因此输出为:

DATE Ascending 1        Away        A Score Home        H Score     
2008-01-01 20:00:00 Saxons 2 Celtics 0
2008-01-01 20:00:00 Spurs 0 Wanderers 1
2008-01-08 20:00:00 Saxons 1 Spurs 2
2008-01-08 20:00:00 Celtics 1 Wanderers 1

最佳答案

您需要与“teams”表再次连接才能获得结果。所以类似的事情:

SELECT
games.DATE,
teams_away.tname AS "Away",
games.ascore AS "A Score",
teams_home.tname AS "Home",
games.hscore AS "H Score"
FROM
games
INNER JOIN teams teams_away ON games.ateam = teams_away.id
INNER JOIN teams teams_home ON games.hteam = teams_home.id
ORDER BY
games.DATE ASC

关于MYSQL查询显示客队名称和主队名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263459/

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