vendredi 18 septembre 2015

SQL - Add "Not IN" to a filterable search results

I am having an issue where I need a search result that shows everything user A does NOT own...the problem is if User B has the same item then it shows up in User A's results because user B owns it. Here is the SQL that solved that problem:

$sql = "SELECT *
FROM iteminfo
WHERE id NOT IN (
    SELECT itemid
    FROM useritems
    WHERE active = 'Y' AND userid = '$userid'

I think the WHERE id NOT IN ( is what solved my problem. However, this won't work for my filter, I need this:

"SELECT * FROM useritems 
JOIN (users, iteminfo) on (users.id=useritems.userid 
AND iteminfo.id=useritems.itemid) 
WHERE  active='y' AND userid!='$userid' ". $brand.$category.$color1.$color2.$pattern.$feature.$fromprice.$toprice." order by useritems.itemid desc LIMIT 40";

but because it doesn't have the NOT IN...it is showing items the user already owns...

Does anyone know how to combine the 2 or fix the second so items the active userid owns doesn't show up in filtered search results?

Aucun commentaire:

Enregistrer un commentaire