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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2003-05-16 : 09:46:32
|
murray writes "hey heyhad a good look through and i can't see this answered anywhere, so i thought i'd post it. :) i've been trying to solve this for hours. i have a many-to-manyrelationship between two entities, PEOPLE and GROUPS. a person can be in many groups. a group can have many people in it. there's a join table here to keep track of this. here's the lay of the land: ------- PEOPLE TABLE -------ID NAME1 john2 alex ------- GROUP TABLE -------ID NAME1 GROUPA2 GROUPB3 GROUPC4 GROUPD ------- JOIN TABLE --------PERSON_ID GROUP_ID1 11 2 what i want to know is which groups are NOT related to 'john' right now: ie.. GROUPC and GROUPD. also - can this query be done just JOINS instead of subqueries.thanks!murray" |
|
|
ToddV
Posting Yak Master
218 Posts |
Posted - 2003-05-16 : 10:04:23
|
| How about this:SELECT A.NameFROM [Group Table] A LEFT JOIN [JOIN TABLE] b ON a.Group_ID = b.Group_ID LEFT JOIN [PEOPLE TABLE] C ON B.PERSON_ID = C.PERSON_ID AND C.Name = 'john'WHERE B.GroupID IS NULL |
 |
|
|
murray
Starting Member
2 Posts |
Posted - 2003-05-17 : 00:19:47
|
| hey there toddthanks for the reply! i just tried it out on this set of data and there seems to be a problem. i have added another join to the join table, connecting the SECOND person with the THIRD group. ------- PEOPLE TABLE -------ID NAME1 john2 alex ------- GROUP TABLE -------ID NAME1 GROUPA2 GROUPB3 GROUPC4 GROUPD ------- JOIN TABLE --------PERSON_ID GROUP_ID1 11 22 3the query result which should show GROUP C and GROUP D as groups unrelated to john now does NOT show GROUP C even though it has nothing to do with john.any advice?thanks in advance :)murrayEdited by - murray on 05/17/2003 00:21:08 |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-05-17 : 11:15:40
|
| DECLARE @People TABLE ([ID] INT, [Name] NVARCHAR(25))INSERT INTO @People([ID],[Name])SELECT 1, 'john' UNION ALLSELECT 2, 'alex'DECLARE @Groups TABLE ([ID] INT, [Name] NVARCHAR(25))INSERT INTO @Groups([ID],[Name])SELECT 1, 'GROUPA' UNION ALLSELECT 2, 'GROUPB' UNION ALLSELECT 3, 'GROUPC' UNION ALLSELECT 4, 'GROUPD' DECLARE @PeopleGroups TABLE (PERSON_ID INT, GROUP_ID INT)INSERT INTO @PeopleGroups(PERSON_ID,GROUP_ID)SELECT 1, 1 UNION ALLSELECT 1, 2 UNION ALLSELECT 2, 3 DECLARE @PersonName NVARCHAR(25)SELECT @PersonName = 'john'SELECT A.[ID], A.[Name]FROM @Groups A LEFT JOIN @PeopleGroups B ON A.[ID] = B.GROUP_ID LEFT JOIN @People C ON B.PERSON_ID = C.[ID] AND C.Name = @PersonNameWHERE C.[Name] IS NULL |
 |
|
|
murray
Starting Member
2 Posts |
Posted - 2003-05-17 : 16:49:06
|
| hey valterthis will work - but i was hoping to keep the query a simple join query and not resort to union stuff! is there no way this query can be made to work as with joins only?all the bestmurray |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2003-05-17 : 17:06:07
|
| I think what Valter was demonstrating is that Todd's answer works correctly.The only unions are the ones used to set up test data. |
 |
|
|
|
|
|
|
|