MySQL JOIN具有相应ID的多个列

时间:2013-07-29 03:18:01

标签: mysql join

我有两个表,一个是团队名称,另一个是用户做出的各种选择,每个表都由团队的team_id表示。我希望能够显示每个团队的实际团队名称,但无法确定联接的工作方式。

表1

user_id(int),selection1(int),selection2(int),selection3(int)

表2

team_id(int),team_name(varchar)

1 个答案:

答案 0 :(得分:1)

您需要三个联接:

select u.user_id, t1.team_name as team_name1, t2.team_name as team_name2,
       t3.team_name as team_name3
from users u left outer join
     teams t1
     on u.selection1 = t1.team_id left outer join
     teams t2
     on u.selection2 = t2.team_id left outer join
     teams t3
     on u.selection3 = t3.team_id;