我有2张这样的表:
[游戏]
游戏ID
hometeamid
awayteamid
得分
和
[小组]
teamid
teamname
如何创建查询以输出如下内容:
[主队] [客场球队] [得分]
49人充电器28-17
你知道,我需要在同一个表中解析2个团队名称和2个团队ID,并输出名称。在此先感谢您的帮助!
答案 0 :(得分:2)
SELECT
ht.TeamName AS HomeTeam,
vt.TeamName AS AwayTeam,
g.Score
FROM
games g INNER JOIN teams ht
on g.hometeamid = ht.teamid
INNER JOIN teams vt
on g.awayteamid = vt.teamid
我建议命名表“游戏”和“团队” - 因为我不是多个表名的粉丝。在这个意见中我并不孤单,但这确实是一种风格/偏好。
答案 1 :(得分:0)
这应该适合你:
select t1.teamname, t2.teamname, g.score from games as g left outer join team as home_team on g.hometeamid = home_team.id left outer join team as away_team on g.awayteamid = away_team.id
答案 2 :(得分:0)
SELECT (SELECT teams.teamname FROM teams WHERE teams.teamid=games.awayteamid) AS awayteam,
(SELECT teams.teamname FROM teams WHERE teams.teamid=games.hometeamId) AS hometeam, score
FROM games
WHERE gameid = '1'