I have three tables -
Users [Id,Name]
Matches_A [Id1,Id2]
Matches_B[Id1,Id2]
I know the user_id of the current user.
For each Id1, in either matches table, there are many entries. That is there are many matches for each user in both tables.
I need to select all of the users who are not in either matches table.
I tried this query -
SELECT * FROM Users
JOIN Matches_A ON Users.id = Matches_A.id2
JOIN Matches_B on Users.id = Matches_B.id2
WHERE Matches_A.id1 != $userid
AND Matches_B.id1 != $userid;
This doesn't work.
The problem is that there are entries in the Matches table with the same id2 but different id1s. That means that even if I exclude a row where the id1 matches the userid, that user (id2) could still be returned because there is another row with the same user where the id1 does not match the user_id.
If that made no sense, let me rephrase it. It would be easy to select everything that I don't want to be returned.
SELECT * FROM Users
JOIN Matches_A ON Users.id = Matches_A.id2
JOIN Matches_B on Users.id = Matches_B.id2
WHERE Matches_A.id1 != $userid
AND Matches_B.id1 = $userid;
Would return all of the rows that I don't want. How can I write a query that will get me all the other rows.
P.S. It's pretty easy to do this with a sub-query, but I'm worried that will be slow especially with 3 tables.