How to insert selected columns from a CSV file to a MySQL database using LOAD DATA INFILE

后端 未结 6 806
甜味超标
甜味超标 2020-11-27 10:13

I have a CSV file which contains 10 columns. I want to select only some columns from that file and load them into a MySQL database using the LOAD DATA INFILE co

6条回答
  •  Happy的楠姐
    2020-11-27 10:45

    Specify the name of columns in the CSV in the load data infile statement.

    The code is like this:

    LOAD DATA INFILE '/path/filename.csv'
    INTO TABLE table_name
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\r\n'
    (column_name3, column_name5);
    

    Here you go with adding data to only two columns(you can choose them with the name of the column) to the table.

    The only thing you have to take care is that you have a CSV file(filename.csv) with two values per line(row). Otherwise please mention. I have a different solution.

    Thank you.

提交回复
热议问题