simple query question

  • situation, I've got two tables

    table 1 named team with columns teamID, name (and some other fields)

    table 2 named game with columns gameID, homeTeamID, awayTeamID, date (and some other fields)

    I want to write a query (and keep on failing after a lot of trying) where I select the team names based on homeTeamID and awayTeamID in the game table but I just can't figure it out.

    I'm stuck with

    SELECT team.name As homeTeam, game.date

    FROM team, game

    WHERE game.homeTeamID = team.teamID

    but how do I get the awayTeam in the same query?

    Thanks in advance for your help and advice

  • tom.weber (9/9/2011)


    situation, I've got two tables

    table 1 named team with columns teamID, name (and some other fields)

    table 2 named game with columns gameID, homeTeamID, awayTeamID, date (and some other fields)

    I want to write a query (and keep on failing after a lot of trying) where I select the team names based on homeTeamID and awayTeamID in the game table but I just can't figure it out.

    I'm stuck with

    SELECT team.name As homeTeam, game.date

    FROM team, game

    WHERE game.homeTeamID = team.teamID

    but how do I get the awayTeam in the same query?

    Thanks in advance for your help and advice

    I would definitely go with something aliases like this

    SELECT hT.name As homeTeam, aT.name as awayTeam, g.date

    FROM game g

    INNER JOIN team hT ON g.homeTeamID = hT.teamID

    INNER JOIN team aT ON g.awayTeamID = aT.teamID

    Ronald HensbergenHelp us, help yourself... Post data so we can read and use it: http://www.sqlservercentral.com/articles/Best+Practices/61537/-------------------------------------------------------------------------2+2=5 for significant large values of 2

  • Wow works like a charm, thanks a lot!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply