Home  >  Q&A  >  body text

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

<p>I have a CSV file with 10 columns. I just want to select some columns from this file and load them into a MySQL database using the <code>LOAD DATA INFILE</code> command. </p>
P粉549412038P粉549412038418 days ago452

reply all(1)I'll reply

  • P粉232409069

    P粉2324090692023-08-30 11:31:45

    Load data into a table in MySQL and specify columns:

    LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE t1 
    FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'  
    (@col1,@col2,@col3,@col4) set name=@col4,id=@col2 ;

    @col1,2,3,4 are variables that save csv file columns (assuming 4) name,id are table columns.

    reply
    0
  • Cancelreply