MySQL how to specify string position with LOAD DATA INFILE

后端 未结 3 622
抹茶落季
抹茶落季 2021-01-28 14:20

I have ASCII files with a static number of characters for each line with no delimiters. I\'d like to use LOAD DATA INFILE to import into my table.

Example of file:

3条回答
  •  一整个雨季
    2021-01-28 15:12

    Another way to do this is just assigning a variable and splitting it direct in your load.

    LOAD DATA INFILE 'yourfilegoeshere' INTO TABLE main_table
      LINES TERMINATED BY '\r\n' (@_var)
    set
      field1=TRIM(SUBSTR(@_var from 1 for 2)),
      field2=TRIM(SUBSTR(@_var from 3 for 2)),
      field3=TRIM(SUBSTR(@_var from 5 for 70));
    

    Just be sure not to specify any field separator, otherwise you will have to use more variables, note that I'm using TRIM to clean data in the same statement.

提交回复
热议问题