I’m trying to run a SQL query to update a table if an ID does not exists in another table.
Heres the query:
UPDATE product
SET active = 0
WHERE id NOT IN (SELECT product_id FROM image);
This query works as intended on my local server(running apache/mysql).
But on my dedicated server(also running apache/mysql), it just returns this:
Query OK, 0 rows affected (0.19 sec)
Rows matched: 0 Changed: 0 Warnings: 0
But I know it should effect some rows.
I’ve double checked everything and yet can’t see why the query wont go through correctly.
Is there some kind of setting I’m missing out perhaps?
Kind regards,
Daniel
Do you have some rows where product_id is null? You must filter these out when using NOT IN:
The reason is that
x NOT IN ycan only befalseorNULLbut nevertrueif y contains aNULLvalue.You could also use one of the other approaches to find values that occur in one table but not in another. For example, using EXISTS:
This approach does not have the same problem with
NULLvalues.