SQL从查询结果中替换另一个表中的多个变量 [英] SQL Replace multiple variables from another table in query result

查看:462
本文介绍了SQL从查询结果中替换另一个表中的多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的团队时间表表:

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

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

尝试执行此操作: 如何更换1&附表2中的怪兽"和查询结果中的臭虫叮咬"?

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

推荐答案

基本上只进行两个联接,一个联接为家名,另一个联接为走人名.

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

我添加了反引号,因为schedule是一个关键字,所以为了不弄乱任何内容,您应该在表名上使用反引号

i added backticks because schedule is a keyword so just to not mess anything up you should use backtics on the tablename

演示

这篇关于SQL从查询结果中替换另一个表中的多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆