SELECT G.groupID, G.groupName FROM `groups` G, `groupMembers` M WHERE M.userID = '$sessionUserID' OR M.userID = '$profileUserID' AND G.groupID = M.groupID GROUP BY M.groupID LIMIT 0,99; Code (SQL): Can AND and OR be used in one WHERE clause? What I'm trying to do is SELECT the list of group names that are shared between two users according to their userID. So one user would be viewing another users profile. $profileUserID and the user viewing would have a session variable $sessionUserID How do I make this work.
Sure, OR and AND can be used together, but use brackets to group them. I think your query could be as follows (I think GROUP BY is not applicable): SELECT G.groupID, G.groupName FROM `groups` G, `groupMembers` M WHERE (M.userID = '$sessionUserID' OR M.userID = '$profileUserID') AND G.groupID = M.groupID LIMIT 0,99; Code (SQL):
wmtips' query looks good! and he is right, the Group By is not needed. It is only used with the aggregate functions of mysql...like COUNT, SUM, etc...
Yup. Brackets are not necessary if you remember that AND has bigger priority rather then OR. But query looks more clear if brackets exist.