I need to select records which exist in users table but don’t exist in tables groups_members, groups_members_unapproved and groups_invitations.
$_POST[‘msg_id’] contains multiple user IDs and I don’t know how to use implode statement in 3 subqueries, so I replaced it with ??? – question marks.
Thanks.
$result = mysql_query("SELECT id FROM users
WHERE id IN (". implode(', ', $_POST['msg_id']).")
AND id NOT IN (SELECT memberid FROM groups_members where memberid='???' and groupid='$cid')
AND id NOT IN (SELECT memberid FROM groups_members_unapproved where memberid='???' and groupid='$cid')
AND id NOT IN (SELECT invited FROM groups_invitations where invited = '???' and groupid='$cid')",$gb1) or die('Error');
What you are likely looking to do is a LEFT OUTER JOIN like this
The thought here being that the outer join will return records across all joined tables even when there are not matching id’s, but that the
* IS NULLlines in the where clause will limit those records to only the cases where there are not matching member id’s between the user table and any of the other tables.I wasn’t quite sure what value the
groupidwas bringing in your query. I would think that if you are concerned with identifying records at the user level that it would be an unnecessary field to filter on.This should be a more efficient query than using subselects, so long as all your ID fields are indexed. Of course if this is a query which you expect to execute frequently, you may want to consider a schema change to get an even more optimized query.