i'm trying to join table Group on my link table userGroup, to get the groups where user with id 30 is not a member, but i can't seem to get it right.
joining with no conditions returns this
SELECT Groups.ID, Groups.Name, UserGroup.groupID, UserGroup.userID
FROM Groups LEFT OUTER JOIN UserGroup ON Groups.ID = UserGroup.groupID
+----------+------------+-------------------+------------------+ | Group.ID | Group.Name | UserGroup.GroupID | userGroup.UserID | +----------+------------+-------------------+------------------+ | 1 | g1 | 1 | 30 | | 2 | g2 | NULL | NULL | +----------+------------+-------------------+------------------+
however when i try to sourt out the row where userID = 30, i'm getting two equally wrong results. adding the condition in the join part returns both rows but claiming the userGroup.userId is null. i'm suspecting some duplicate rows due to join, but haven't been able to figure out what exactly and how to fix it
SELECT Groups.ID, Groups.Name, UserGroup.groupID, UserGroup.userID
FROM Groups LEFT OUTER JOIN UserGroup ON Groups.ID = UserGroup.groupID AND UserGroup.userID <> 30
+----------+------------+-------------------+------------------+ | Group.ID | Group.Name | UserGroup.GroupID | userGroup.UserID | +----------+------------+-------------------+------------------+ | 1 | g1 | NULL | NULL | | 2 | g2 | NULL | NULL | +----------+------------+-------------------+------------------+
adding the condition to the very end of my statement, returns 0 rows.
SELECT Groups.ID, Groups.Name, UserGroup.groupID, UserGroup.userID
FROM Groups LEFT OUTER JOIN UserGroup ON Groups.ID = UserGroup.groupID
WHERE UserGroup.userID <> 30
i'm running on an mssql server and using c# SqlCommand to execute the query, but i don't think that's connected to the issue
ok
try using<>
– vhadalgi