简体   繁体   中英

mysql not in rewrite query issue

I have a query with NOT IN . I've heard MySQL doesn't support NOT IN , but I can't figure how to rewrite it.

My query is:

SELECT * FROM quests
  WHERE id NOT IN ('" .$quest_completed. "') 
  AND location=" .$location_id. "
  AND (follows=0 OR follows IN ('" .$quest_completed. "')) 
  ORDER BY title

eg:

SELECT * FROM quests 
WHERE id NOT IN (6,21) AND 
  location=8 AND 
  (follows=0 OR follows IN (6,21))
ORDER BY title

Row id 6 is being returned in that when I specified WHERE id NOT IN 6

It looks like, because of your use of apostrophes around the $quest_completed variable, the query that's actually being executed is likely to be NOT IN ('6,21') , rather than NOT IN (6,21) (which I think is what you want?).

The difference is that the first version returns records where id is not equal to the string '6,21' , whereas the second version returns records where id is neither the number 6 nor the number 21 .

MySQL supports NOT IN just fine . A restriction only applies to certain cases, like the one here: MySQL "NOT IN" query

NOT IN can also be interpreted as logical NOT (some condition) such as

NOT ID in ( 'A', 'B', 'C', 'D' )

SO, if you are having issues as written, just try swapping ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM