我有一个团队时间表表,如下所示:
谢德勒
Game1_Time | Game1_Home_Team | Game1_Away_Team =================================================== 12:00:00 | 1 | 2
我想用另一个表中存在的相应团队替换团队值:
DBO团队
Team_Number | Team_Name ======================== 1 | The Monsters 2 | Bug Bites
尝试执行此操作:如何在查询结果中用“ The Monsters”和“ Bug Bites”替换Schedule中的1和2?
Game1_Time | Home Team | Away Team =================================================== 12:00:00 | The Monsters | Bug Bites
基本上只是做两个连接,一个连接为家乡名称,一个连接为客场名称。
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是一个关键字,所以为了不弄乱任何内容,您应该在表名上使用反引号
演示