Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple Foreign Keys to Primary Key

Author  Topic 

marcdd2
Starting Member

2 Posts

Posted - 2010-04-05 : 20:17:47
I have a table, Games, with Baseball games that includes two columns HomeTeamID and AwayTeamID. I want to use PHP to query the DB and pull the Teams names from a second table, Teams, which has two columns TeamID and TeamName. Is this possible or do I need to create two identical tables, HomeTeams and Away Teams? Can someone help me with what the SQL Select query should be or even better the php line?

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-05 : 20:27:52
[code]SELECT h.TeamName AS HomeTeam, a.TeamName AS AwayTeam
FROM Games g
INNER JOIN Teams h
ON g.HomeTeamID = h.TeamID
INNER JOIN Teams a
ON g.AwayTeamID = a.TeamID
[/code]

There are 10 types of people in the world, those that understand binary, and those that don't.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-05 : 22:31:46
Please note that this is a Microsoft SQL Server site, so any solutions that are provided may not work on databases that aren't MSSQL. PHP typically uses MySql as the backend. The above post will probably work though in any dbms. I just wanted to alert you of this for future questions.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

marcdd2
Starting Member

2 Posts

Posted - 2010-04-06 : 18:06:25
Thanks for the solution and head up. Didn't notice that it was MSSQL.
Go to Top of Page
   

- Advertisement -