select / join / t-sql

时间:2015-01-06 16:58:57

标签: sql-server tsql select join

我有两张桌子

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

我需要这样的表格:

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

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

我假设您的第一个表名为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