SQL在查询结果中替换另一个表中的多个变量

时间:2014-08-18 14:07:17

标签: mysql sql

我有一个团队日程表,如下所示:

DBO.SCHEDULE

Game1_Time  |  Game1_Home_Team   | Game1_Away_Team 
===================================================
12:00:00    |         1          |         2

我想将团队价值替换为另一个表中存在的相应团队:

DBO.TEAM

Team_Number  |  Team_Name
========================
    1        |  The Monsters
    2        |  Bug Bites

尝试这样做: 如何更换1& 2,附表" The Monsters" &安培; " Bug Bites"在查询结果中?

Game1_Time  |  Home Team         | Away Team 
===================================================
12:00:00    |  The Monsters      |  Bug Bites

1 个答案:

答案 0 :(得分:6)

基本上只需要两个连接一个用于主页名称,一个连接用于离开名称。

SELECT 
     s.Game1_Time, 
     t.Team_Name as 'Home Team', 
     t1.Team_Name as 'Away Team'
FROM `SCHEDULE` s
JOIN `TEAM` t on t.Team_Number = s.Game1_Home_Team
JOIN `TEAM` t1 on t1.Team_Number = s.Game1_Away_Team

我添加了反引号,因为schedule是一个关键字,所以只是为了不弄乱你应该在tablename上使用backtics

DEMO