How I can Import data from CSV to MySQL?

前端 未结 2 1092
情歌与酒
情歌与酒 2020-12-02 01:18

I have csv file with following structure:

    A   BA0011  U206    NAME    0000000000000149.00     000000.00  0000000000000118.93  S   N   N
    A   BB0011  U         


        
相关标签:
2条回答
  • 2020-12-02 01:35

    You can specify the columns and mark the unnecessary columns as '@dummy'.

    LOAD DATA INFILE 'data.csv'
    INTO TABLE t1
    (column1, @dummy, column2, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    FIELDS TERMINATED BY '\t' ENCLOSED BY 
    LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES;
    

    Replace t1, column1 and column2 as you like. To set other columns which are not in the data file, you can do it like this:

    LOAD DATA INFILE 'data.csv'
    INTO TABLE t1
    (column1, @dummy, column2, @dummy, @val, @dummy, @dummy, @dummy, @dummy, @dummy)
    FIELDS TERMINATED BY '\t' ENCLOSED BY 
    LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES
    SET column3 = "test", column4 = CURRENT_TIMESTAMP, column5 = @val/10;
    

    For further reference, I recommend you to take a look at the MySQL reference.

    0 讨论(0)
  • 2020-12-02 01:48

    Perhaps you could import it into a table that has all the columns (to match the CSV), then select-insert it into your target table?

    0 讨论(0)
提交回复
热议问题