user1260310
user1260310

Reputation: 2227

php/mysql join syntax

I have the userids of people who have joined a group in one table but not their names thatlie in another table. So I think I need to do a join. I'm starting with groupid that describes the grouop.

Table 1, groupmem has groupid and userid for the members.

Table 2, users has userid and username

The users table has every user. The groupmem only has some who have joined groups.

SQL statement should be something like following but can't get it to work.

select users.name
FROM users,groupmem
LEFT JOIN users
on groupmem.userid=users.userid
WHERE groupmem.groupid = 22

22 being some value..

Thinking maybe where clause is in wrong place or I am using wrong type of join but or not using on correctly but, in any case, can't get it to work. Thx for any suggestions:

Upvotes: 0

Views: 458

Answers (2)

SenorAmor
SenorAmor

Reputation: 3345

Try:

SELECT u.username
FROM `users` u
LEFT JOIN `groupmem` g
ON u.userid = g.userid
WHERE g.groupid = 22

Upvotes: 1

user1048311
user1048311

Reputation:

This should do the trick:

SELECT users.name
FROM groupmem
LEFT JOIN users ON groupmem.userid = users.userid
WHERE groupmem.groupid = 22

Your query seems to be quite right - assuming I guessed your table-structure right.

Upvotes: 0

Related Questions