I am having a problem when I uploading a CSV file to MySQL. I want to update the data that is present. Here is some example CSV content:
John,20,1977
Mary,22,1989
In my MySQL database I have:
John|18|1977
Mary|22|1990
When I upload the CSV file to my MySQL database I want to update the data instead of adding new data. I use PHPMyAdmin to upload the CSV file.
Directly you can't update.
First you need to import data in a temp table. Then compare main table data with temp table data and update using update
join
query.
Step 1) Create another table to use for the import
CREATE TABLE temp_table_import LIKE name_maintable;
Step 2) Perform the import into temp_table_import
LOAD DATA INFILE 'somefile.csv' INTO TABLE temp_table_import ...
Step 3) Perform an UPDATE JOIN
UPDATE name_maintable A
INNER JOIN temp_table_import B USING (id)
SET A.columnname= B.columnname;
You can you REPLACE keyword with LOAD DATA for more detail click here
UPDATE name_maintable B
INNER JOIN temp_table_import A USING (id)
SET A.columnname= B.columnname;
The last line should read
SET B.columnname= A.columnname;
as you want to update table B not table A