在MySQL中加入两个表或选择查询

时间:2013-10-03 18:04:36

标签: mysql sql join

我需要连接两个表并获得如下所述的输出, enter image description here

这里我的问题是我无法内连接两个表并获得输出,因为它引用了team1和team2的相同列名(看起来像“select ct.teamname,ct2.teamname from clm_schedule cs”其中它给了我一个错误)。我可以在两个单独的查询中完成,如下所示,

select ct.teamname as team1 from clm_schedule cs inner join clm_team ct on ct.teamid = cs.team1

select ct2.teamname as team2 from clm_schedule cs2 inner join clm_team ct2 on ct2.teamid = cs2.team2

但是我无法加入它来获得单个结果集。请建议我一个方法。

1 个答案:

答案 0 :(得分:2)

你应该可以这样做:

SELECT ct1.teamname as team1 ,ct2.teamname as team2 
FROM clm_schedule cs 
LEFT JOIN clm_team AS ct1 
  ON ct1.teamid = cs.team1
LEFT JOIN clm_team AS ct2
  ON ct2.teamid = cs.team2;