Import CSV to Update only one column in table

前端 未结 3 1614
忘了有多久
忘了有多久 2020-11-30 17:38

I have a table that looks like this:

products
--------
id, product, sku, department, quantity

There are approximately 800,000 entries in th

3条回答
  •  离开以前
    2020-11-30 18:16

    Answer from @ike-walker is indeed correct but also remember to double check how your CSV data if formatted. Many times for example CSV files can have string fields enclosed in double quotes ", and lines ending with \r\n if working on Windows.
    By default is assumed that no enclosing character is used and line ending is \n. More info and examples here https://mariadb.com/kb/en/importing-data-into-mariadb/

    This can be fixed by using additional options for FIELDS and LINES

    CREATE TEMPORARY TABLE your_temp_table LIKE your_table;
    
    LOAD DATA INFILE '/tmp/your_file.csv'
    INTO TABLE your_temp_table
    FIELDS 
       TERMINATED BY ','            
       OPTIONALLY ENCLOSED BY '"'    -- new option
    LINES TERMINATED BY '\r\n'       -- new option
    
    (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;
    

提交回复
热议问题