matches ( int user1id, int user2id ) users ( userid, int group, varchar(50) type ) SELECT ALL USERS (1st user) for each user, select * from students where group<>$thisusersgroup AND type<>$thisuserstype (2nd user) want to check that if user1id is the 1st user and user2id is the 2nd user, this user isn't in the selectable set similarly if user2id is the 1st user and user1id is the 1st user