Updating MySQL Databasefields via load local infile

A way of using the LOAD DATA INFILE command to process a CSV file to UPDATE all rows in a table using the primary key.

You want to update rows in a MySQL-Table, using the contents of a CSV-File. This example describes updating prices in a products table, using a idetically temporary table and a CSV file, which contains exactly three, fields: product ID and price (assuming, product ID is primary) and state. Obviously, the number of fields to be updated can be extended. First create a temporary table with the LIKE statement
CREATE TEMPORARY TABLE temp_products LIKE products;
Load data infile into temporary table
LOAD DATA LOCAL INFILE '/home/user/customers/some/updatefile.csv' INTO TABLE temp_products FIELDS TERMINATED BY ';' ENCLOSED BY '"' (id, price, state);
UPDATE products using the INNER JOIN statement
UPDATE products INNER JOIN temp_products on temp_products.id = products.id SET products.price = temp_products.price, products.state = temp_products.state;
finally delete temporary table
DROP TEMPORARY TABLE temp_products;