I have a SQLite database, which needs to be updated.
Using the update statement this should be easy.
But things aren’t going as planned:
This is the complete query:
UPDATE products_new SET name =
(
SELECT products_sap.title
FROM products_sap, products_new
WHERE products_sap.id_a = products_new.product_id
);
As part of my debugging scheme I’m trying out each (sub)query.
This query ( identical to the one used in the above update )
shows me the correct records:
SELECT products_sap.id, products_sap.title
FROM products_sap, products_new
WHERE products_sap.id_a = products_new.product_id;
Example:
id | title
----------------------
1 | Lorum Ipsum
2 | Lorum Not Ipsum
3 | Ipsum Lorum
But: all my rows in the updated table have identical name values * gasps *.
SELECT products_new.name from products_new;
Example:
id | name
----------------------
1 | Lorum Ipsum
2 | Lorum Ipsum
3 | Lorum Ipsum
So my question is:
how can I updated each row with it’s relevant name value?
That happens because the
products_newtable in the inner query is hiding theproducts_newtable in theUPDATEclause. Thus, you never actually join the rows from the table you are updating.You need to make sure that for each row you are trying to update, its id (
product_id) is joined with the table (products_sap) that contains the new value: