Sackling Sackling - 1 year ago 78
MySQL Question

updating table only where there is a key match

at first I wanted to update a table (product prices) by importing a csv file but I don't think that is possible.

So I created a temporary table and imported 2 columns: products_id, products_price

I think tried running this sql:

UPDATE products p SET p.products_price=(
SELECT t.products_price
FROM temp_table t
WHERE t.products_id=p.products_id

This updated all the prices where there was a products_id match but also set all the other products to 0.00

What can I do to only update when there is a match and ignore other product_ids?

Answer Source

You can use a update join query like

UPDATE products p 
JOIN temp_table t ON t.products_id=p.products_id
SET p.products_price = t.products_price;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download