If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Stuck on a join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-28-09, 13:09
Eiolon Eiolon is offline
Registered User
 
Join Date: Jan 2006
Posts: 26
Stuck on a join

I have two tables, games and teams. In the games table I have team1 and team2 which are referencing the teams table to get the name of the team.

I always get a empty set returned.

My query:

Code:
SELECT g.team1, g.team2, t.team FROM games g JOIN teams t ON (g.team1 = t.id AND g.team2 = t.id);
if I leave out the AND g.team2 = t.id I can get the first team name but I can't figure out how to get the second name.
Reply With Quote
  #2 (permalink)  
Old 08-28-09, 13:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
tip: learn indenting and line breaks

Code:
SELECT t1.team AS team1
     , t2.team AS team2 
  FROM games AS g 
INNER 
  JOIN teams AS t1 
    ON t1.id = g.team1
INNER
  JOIN teams AS t2
    ON t2.id = g.team2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-28-09, 14:15
Eiolon Eiolon is offline
Registered User
 
Join Date: Jan 2006
Posts: 26
Thanks very much.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On