0

I have two tables, one with names of teams, the other holds various selections made by users, each of which is represented by the team's team_id. I want to be able to display the actual team names for each, but can't figure out how the join would work.

Table 1

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

Table 2

team_id(int), team_name(varchar)

1 Answer 1

1

You need three joins:

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;
Sign up to request clarification or add additional context in comments.

2 Comments

what do t1, t2, and t3 represent? there's only one teams table.
@KyleHagler . . . They are table aliases, and an important part of the SQL language. For instance, they let you use the same table several times in a query -- when you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.