Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

mysql - SQL Replace multiple variables from another table in query result

I have a team schedule table that looks like this:

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

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?

Game1_Time  |  Home Team         | Away Team 
===================================================
12:00:00    |  The Monsters      |  Bug Bites
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

DEMO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...