How to skip columns in CSV file when importing into MySQL table using LOAD DATA INFILE?

前端 未结 4 1444
猫巷女王i
猫巷女王i 2020-11-27 11:58

I\'ve got a CSV file with 11 columns and I have a MySQL table with 9 columns.

The CSV file looks like:

col1, col2, col3, col4, col5, col6, col7, col         


        
相关标签:
4条回答
  • 2020-11-27 12:28

    @deemi:

    The only way to ignore the @dummy is by setting the field's Default to AUTO INCREMENT. So you can skip the field and just code like this,

    LOAD DATA INFILE 'file.txt'  
    INTO TABLE t1 (column2, column3, column4, column5);
    

    //assumes that the fieldname column1 is set to AUTO INCREMENT by default.

    0 讨论(0)
  • 2020-11-27 12:33

    I think there is one more change in the code:

    The following SQL command:

    LOAD DATA LOCAL INFILE 'filename.csv' INTO TABLE my_table
    FIELDS TERMINATED BY ','
    ENCLOSED BY ''
    LINES TERMINATED BY '\n'
    

    -will probably result in an data truncation error.

    So it is better to use LINES TERMINATED BY '\r\n' instead of LINES TERMINATED BY '\n'

    SO the code will be:

    LOAD DATA LOCAL INFILE 'filename.csv' INTO TABLE my_table
    FIELDS TERMINATED BY ','
    ENCLOSED BY ''
    LINES TERMINATED BY '\r\n'
    
    0 讨论(0)
  • 2020-11-27 12:37

    From Mysql docs:

    You can also discard an input value by assigning it to a user variable and not assigning the variable to a table column:

    LOAD DATA INFILE 'file.txt'  
    INTO TABLE t1 (column1, @dummy, column2, @dummy, column3);
    
    0 讨论(0)
  • 2020-11-27 12:47

    step1.deal with awk.

    cat file.txt |awk '{print $1,$2,$5...}'>new_file.txt
    

    step2.load into mysql.

    load data local infile 'new_file' into table t1(...)
    

    the method below is simple,but not allowed in lower version of mysql.

    LOAD DATA INFILE 'file.txt'  
    INTO TABLE t1 (column1, @dummy, column2, @dummy, column3);
    
    0 讨论(0)
提交回复
热议问题