469,111 Members | 1,923 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,111 developers. It's quick & easy.

SQL many-to-many query problem. HELP !

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.
Marvin
Jul 20 '05 #1
2 1174
SELECT S.sport_id, S.sport_name
FROM tblSports AS S
WHERE NOT EXISTS
(SELECT *
FROM tblJoin
WHERE sport_id = S.sport_id
AND team_id = ???)

SELECT T.team_id, T.team_name
FROM tblTeams AS T
WHERE NOT EXISTS
(SELECT *
FROM tblJoin
WHERE team_id = T.team_id
AND sport_id = ???)

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
It WORKS!
Thanks a lot.

:)
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by YoBro | last post: by
3 posts views Thread by luis.c.torres | last post: by
1 post views Thread by hardik | last post: by
29 posts views Thread by wizofaus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.