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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SQL many-to-many query problem - 3 tables - HELP!

Author  Topic 

joe_fun
Starting Member

2 Posts

Posted - 2004-07-15 : 11:30:17
I have 3 tables:

1. tblSports which has an ID and Sport columns (basketball, baseball, football, and volleyball)

2. tblTeams with an ID and Team columns (TeamsA to D)

3. tblJoin which has two foreign key columns (ID for Sports, ID for Teams) This is where teams are joined to whatever Sports they play.

Questions:

1. How should I do my select statement when given the team
it'll show the sports they do NOT play?

2. Just the opposite of the 1st. Given the sport, what teams do NOT play?

Thanks in advance for your help.

drymchaser
Aged Yak Warrior

552 Posts

Posted - 2004-07-15 : 11:54:41
What is this question for? Why are you trying to answer this here?
Go to Top of Page

joe_fun
Starting Member

2 Posts

Posted - 2004-07-15 : 13:46:52
Nevermind. I figured it out myself.
I hope this helps someone later on.

Given the sportID of 1:


SELECT tblTeam.ID, tblTeam.Team
FROM (SELECT *
FROM tblJoin INNER JOIN
tblTeam ON tblJoin.TeamID = tblTeam.ID
WHERE (tblJoin.SportID = '1')) tblResult RIGHT OUTER JOIN
tblTeam ON tblResult.ID = tblTeam.ID
WHERE (tblResult.SportID IS NULL)
Go to Top of Page
   

- Advertisement -