Jump to content

Yet another question on JOIN, please help


nathank

Recommended Posts

I have been reading up on joins, but I still am a bit unclear how I would write my query to do the following.

 

I have a table named teams

|  ID  |  Teamname        |
|  1    |  The Good Team |
|  2    |  The Bad Team   |

 

And a table named matchups

|  ID  |  Team1  |  Team2  |
|  1    |     1       |      2      |

 

I want to query the matchups table and pull the teamnames from the teams table.

 

"SELECT matchups.team1, teams.teamname as team1, matchups.team2, teams.teamname as team2 FROM matchups join teams on matchups.team1 = teams.id "

 

The join has one team id = to the id in the team table, but what about the other team?

");

Link to comment
Share on other sites

With this table structure I would try something like this:

 

SELECT A.team1

,B.teamname as team1

,A.team2

,C.teamname as team2

 

FROM matchups A

JOIN teams B

ON A.team1 = B.id

JOIN teams C

ON A.team2 = C.id

 

/*A B and C are table aliases which I used just to cut down on typing*/

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.