我有两张表,一张包含团队名称,另一张包含用户所做的各种选择,每个选择都由团队的 team_id 表示。我希望能够显示每个团队的实际名称,但无法弄清楚加入是如何工作的。
表格1
user_id(int), selection1(int), selection2(int), selection3(int)
表 2
team_id(int), team_name(varchar)
您需要三个连接:
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;