2015-04-03 80 views
0

我有两个表象下面这样:列字段加入在MySQL查询

表1

teamid teamname 
1  AAA 
2  BBB 
3  CCC 

表2

id team1 team2 
1 1  2 
2 2  3   
3 1  3 

表2包含两个字段TEAM1team2参考表1 teamid

预期结果:

id team1 team2 
1 AAA  BBB 
2 BBB  CCC 
3 AAA  CCC 

回答

1

你需要加入table1 2倍

select 
t2.id, 
t1.teamname as team1, 
t11.teamname as team2 
from table2 t2 
join table1 t1 on t1.teamid = t2.team1 
join table1 t11 on t11.teamid = t2.team2 
+0

感谢@abhik。还有一个问题,假设我有这样的第三个表,我想在查询中加入第三个表有可能吗? – 2015-04-03 07:43:45

+0

是可能的。更新问题并添加所需输出的详细信息。 – 2015-04-03 07:44:58