PDA

View Full Version : Help with Join


BestWD
8-23-06, 11:17 AM
I need help figuring out a table join. I don't understand why it's not giving me the results I am looking for, the statement makes perfect sense to me! haha

The two tables I want to join are 'users' and 'members', that look something like this:

users members
|----------------| |-----------------------------|
| Id | name | | Id | userid | groupid |
|----------------| |-----------------------------|
| 1 | Joe | | 26 | 1 | 17 |
| 2 | Sue | | 27 | 4 | 17 |
| 3 | John | | 28 | 2 | 17 |
| 4 | Rick |
| 5 | Sally |

One join I got to work was to find all users info who are members of the group 17. The statement:

select users.* from users left join members on users.id=members.userid where members.groupid=17;

This returns the correct results:
1 Joe
2 Sue
4 Rick

Now I want to get the opposite results--users who are not in group 17. I thought that this statement would work:

select users.* from users left join members on users.id<>members.userid where members.groupid=17;

But instead I get this:
1 Joe
1 Joe
2 Sue
2 Sue
3 John
3 John
3 John
4 Rick
4 Rick
5 Sally
5 Sally
5 Sally

Why is it returning ALL users, and duplicates?? I don't get it!!

Does anyone know how I can get the desired results (only John and Sally)??

Thanks!
Sandy

mitchind
8-23-06, 03:15 PM
Small change - I think this will work for you:

select users.* from users left join members on users.id=members.userid where members.groupid<>17;