Opposite of MySQL FIND_IN_SET
31,909
Solution 1
FIND_IN_SET
returns the index of the match if it is found, and returns 0 if it is not found. Since 0 is FALSE you can just use NOT FIND_IN_SET('needle', 'haystack')
Solution 2
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set
FIND_IN_SET('needle', haystack) = 0
should do the trick.
Solution 3
SELECT id FROM table where !FIND_IN_SET(needle,haystack).......
Its working for me...
Solution 4
It seems like it doesn't work if the field is NULL and therefore doesn't contain the value.
A workaround:
WHERE id NOT IN (SELECT id FROM table WHERE FIND_IN_SET(needle,haystack))
Hope it'll help!
![phirschybar](https://i.stack.imgur.com/VCVqD.jpg?s=256&g=1)
Author by
phirschybar
I have an undying passion for making useful, digital tools that help people kick ass. I also like to make things that have no use, but are interesting or thought-provoking.
Updated on July 13, 2022Comments
-
phirschybar almost 2 years
How can I do the equivalent of:
!FIND_IN_SET('needle', haystack)
-
Nicola Cossu almost 13 yearsI agree with you but even his solution with ! instead of not does the trick. So I don't understand what's his problem :)
-
Jeff Wilbert over 4 yearsA note to anyone who may come across this question/solution, the use of
NOT FIND_IN_SET(...)
will only work correctly if your needle and haystack don't containNULL
values. It's a case where logically reading the query it sounds correct, but following the return values will find it doesn't work since the function returnsNULL
if either param values contain aNULL
value. The proper way would beFIND_IN_SET(...) IS NULL
to check forNULL
BUT if your haystack could contain bothNULL
and and empty string''
; then just doNOT FIND_IN_SET(needle, IFNULL(haystack, '')
.