I have a table that looks like this:
products -------- id, product, sku, department, quantity
There are approximately 800,000 entries in this table. I have received a new CSV file that updates all of the quantities of each product, for example:
productA, 12 productB, 71 productC, 92
So there are approximately 750,000 updates (50,000 products had no change in quantity).
My question is, how do I import this CSV to update only the quantity based off of the product
(unique) but leave the sku
, department
, and other fields alone? I know how to do this in PHP by looping through the CSV and executing an update for each single line but this seems inefficient.
CSV file must be in same order of the table column, put all your columns and no column name. Then in phpMyAdmin, go to the table of database, click import. Select CSV in the drop-down of Format field. Make sure "Update data when duplicate keys found on import (add ON DUPLICATE KEY UPDATE)" is checked.
Here are the steps to get an automatic refresh from the csv file: From a blank work select "From Text" in the "Get External Data" section of the Data tab. Use the Text Import Wizard to set how your csv file will be imported. After you select Finish to exit the Import Wizard, a dialog box titled Import Text will come up ...
When you open it in your spreadsheet editor (Excel, for example), it shows all of the data in a single column, instead of splitting them across columns. The reason behind this behavior is that CSV files (Comma Separated Values) are splitting data into columns using " , " as separator by default.
Unlike other spreadsheet files, CSVs only carry a single sheet, with data fields most often separated by commas. They can store strings of numbers and words but not formulas and formatting styles.
You can use LOAD DATA INFILE
to bulk load the 800,000 rows of data into a temporary table, then use multiple-table UPDATE
syntax to join your existing table to the temporary table and update the quantity values.
For example:
CREATE TEMPORARY TABLE your_temp_table LIKE your_table; LOAD DATA INFILE '/tmp/your_file.csv' INTO TABLE your_temp_table FIELDS TERMINATED BY ',' (id, product, sku, department, quantity); UPDATE your_table INNER JOIN your_temp_table on your_temp_table.id = your_table.id SET your_table.quantity = your_temp_table.quantity; DROP TEMPORARY TABLE your_temp_table;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With