select/加入/t-sql

select / join / t-sql

我有两个table

NumberOfTeam    NameOfTeam
1               Roma
2               Manchester
3               Inter
4               Milan

Game    FirstTeam   GoalsFirstTeam  SecondTeam  GoalsSecondTeam
1       1           1               2           3
2       3           0               4           0

我需要 table 这样的:

Game    FirstTeam   GoalsFirstTeam  SecondTeam  GoalsSecondTeam
1       Roma        1               Manchester  3
2       Inter       0               Milan       0

有人可以帮我解决这个问题吗?

我假设您的第一个 table 称为 Teams,第二个 Games:

SELECT g.Game,
        t1.NameOfTeam AS FirstTeam,
        g.GoalsFirstTeam,
        t2.NameOfTeam AS SecondTeam,
        g.GoalsSecondTeam
FROM Games g
INNER JOIN Team t1 ON g.FirstTeam = t1.NumberOfTeam
INNER JOIN Team t2 ON g.SecondTeam = t2.NumberOfTeam