简体   繁体   English

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

[英]SQL Replace multiple variables from another table in query result

I have a team schedule table that looks like this: 我有一个团队日程表,如下所示:

DBO.SCHEDULE DBO.SCHEDULE

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

I want to replace the team values with their corresponding team that exists in another table: 我想将团队值替换为另一个表中存在的相应团队:

DBO.TEAM DBO.TEAM

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

TRYING TO DO THIS: How do I replace the 1 & 2 in Schedule with "The Monsters" & "Bug Bites" in a query result? 尝试这样做:如何在查询结果中用“The Monsters”和“Bug Bites”替换附表中的1&2?

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

basically just do two joins one for the home name and one for the away name. 基本上只做两个连接一个用于主页名称和一个用于离开名称。

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

i added backticks because schedule is a keyword so just to not mess anything up you should use backtics on the tablename 我添加了反引号,因为schedule是一个关键字,所以只是为了不弄乱你应该在tablename上使用backtics

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM