Results 1 to 3 of 3

Thread: Stuck on a join

  1. #1
    Join Date
    Jan 2006
    Posts
    26

    Unanswered: 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.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jan 2006
    Posts
    26
    Thanks very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •