I'm completely stumped and have tried different solutions I've found on SO but nothing has worked. Here is my problem:
I have two tables with completely different structures. One table is for USERS and the other table is for BLOCKS. The goal is to remove any items from the USERS search that appear in the BLOCKS table.
I've tried achieving this through a SQL query using NOT IN but it's only excluding one of items.
I've also tried array_diff in PHP but I'm still seeing the blocked users in the user search results.
I don't know if the table structures have to match or if my SQL is too complex. Any guidance?
Table Structure
> +----------+---------+-----------+
|username |zipcode |birthdate |
+----------+---------+-----------+
|tester55 |72758 |1999-09-09 |
+----------+---------+-----------+
|tester86 |60608 |1983-05-10 |
+----------+---------+-----------+
|iosuser1 |10011 |1975-12-19 |
+----------+---------+-----------+
|iosuser5 |10011 |1975-12-21 |
+----------+---------+-----------+
|tester150 |10511 |1975-12-21 |
+----------+---------+-----------+
Blocks table
+----------+---------+-----------+
|blocker |blockeduser
+----------+---------+-----------+
|tester86 |tester55 | |
+----------+---------+-----------+
|iosuser5 |tester55 | |
+----------+---------+-----------+
|tester150 |tester55 | |
+----------+---------+-----------+
Zip Code table
+----------+---------+-----------+
|zipcode |city
+----------+---------+-----------+
|72758 |Rogers | |
+----------+---------+-----------+
|60608 |Chicago | |
+----------+---------+-----------+
EDIT: Updated query based on feedback from @TomC
SELECT
*
FROM
(
SELECT
zipcodes.zip,
zipcodes.city,
zipcodes.state,
users.id,
users.username,
users.ava,
users.gender,
users.race,
users.headline,
users.marital,
users.height,
users.bodytype,
users.religion,
users.education,
users.occupation,
users.politics,
users.kids,
users.wantkids,
users.favdrink,
users.drink,
users.smoke,
users.interests,
users.aboutme,
users.seekingGender,
users.seekingdistance,
users.seekingrace,
users.seekingmarital,
users.seekingminage,
users.seekingmaxage,
users.seekingminheight,
users.seekingmaxheight,
users.seekingbodytype,
users.seekingreligion,
users.seekingeducation,
users.seekingoccupation,
users.seekingpolitics,
users.seekingkids,
users.seekingwantkids,
users.seekingdrink,
users.seekingsmoke,
users.birthdate,
YEAR(CURRENT_TIMESTAMP) - YEAR(users.birthdate) -(
RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(users.birthdate, 5)
) AS age,
3959 * ACOS(
COS(RADIANS(zipcodes.latitude)) * COS(RADIANS(center.latitude)) * COS(
RADIANS(zipcodes.longitude) - RADIANS(center.longitude)
) + SIN(RADIANS(zipcodes.latitude)) * SIN(RADIANS(center.latitude))
) AS distance
FROM
users AS seeker
JOIN zipcodes AS center
ON
center.zip = seeker.zip
JOIN users ON seeker.zip = users.zip
JOIN zipcodes ON zipcodes.zip = users.zip
WHERE
seeker.username = 'tester55' AND seeker.username <> users.username AND users.gender = seeker.seekingGender AND seeker.gender = users.seekingGender AND users.seekingmarital LIKE seeker.seekingmarital AND users.bodytype LIKE seeker.seekingbodytype AND users.religion LIKE seeker.seekingreligion AND users.education LIKE seeker.seekingeducation AND users.occupation LIKE seeker.seekingoccupation AND users.politics LIKE seeker.seekingpolitics AND users.kids LIKE seeker.seekingkids AND users.wantkids LIKE seeker.seekingwantkids AND users.drink LIKE seeker.seekingdrink AND users.smoke LIKE seeker.seekingsmoke AND users.race LIKE seeker.seekingrace AND seeker.seekingminheight <= users.height AND seeker.seekingmaxheight >= users.height AND users.birthdate >= DATE_SUB(
NOW(), INTERVAL seeker.seekingmaxage YEAR) AND users.birthdate <= DATE_SUB(
NOW(), INTERVAL seeker.seekingminage YEAR) AND NOT EXISTS(
SELECT
*
FROM
blocks
WHERE
where blocks.blockeduser=seeker.username and blocks.blocker=users.username
)
) selections
WHERE
distance < selections.seekingdistance
ORDER BY
distance
blockerandblockeduserreversed on the table ?