I am experiencing a caffeine deprivation attack...
If I have a string of numbers
$myStr = '100, 102, 204';
How do select results from mySQL db that don't have id values matching one of those in the string?
SELECT * FROM t1
WHERE t1.id NOT IN ($myStr)
That just does not look right...
My guess is that you are quoting the full clause:
SELECT * FROM t1
WHERE t1.id NOT IN ('100, 102, 204')
You need this:
SELECT * FROM t1
WHERE t1.id NOT IN (100, 102, 204)
Your code is perfectly correct.
If you just select from a single table, id
instead of t1.id
is enough though.
The SQL IN
clause doesn't allow a single variable to represent a list of values. The query, as-is, can only be run as dynamic SQL -- on any database.
An alternative that is MySQL specific but less restricted is to use the FIND_IN_SET function:
SELECT t.*
FROM T1 t
WHERE FIND_IN_SET(t.id, $myStr) = 0
FIND_IN_SET documentation:
...Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.