Let’s say I have three values in PHP: “a”, “b”, “c”. Doesn’t matter whether in an array or comma separated string.
There is a table in database:
id | value
1 | a
2 | b
3 | d
My purpose is to find the values that are in php array but not in database table.
The given example will give “c”.
Can I do it with only one query?
UPDATE
Received several good suggestions in answers about array_diff(), though in my case the DB table is really large and the array has not more than 5-6 items. So it would be better to perform 5-6 queries, I think.
If the PHP array is short, you can build a
UNION ALLquery to build your small table, then useNOT INorLEFT JOINquery (whichever is faster) against the large table:Alternately, you can insert the php array values in a temporary table and use the
INorJOINqueries. Of course, this means you end up writing three extra queries: