MySQL how to specify string position with LOAD DATA INFILE

后端 未结 3 520
抹茶落季
抹茶落季 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

    First create a temporary table which you will load all lines into it, then you can load the data from the temporary table into the main table and split to fields using substring

    Something like this:

    CREATE TEMPORARY TABLE tmp_lines
      (countrystring TEXT);
    
    LOAD DATA INFILE 'yourfilegoeshere' INTO TABLE tmp_lines
      FIELDS TERMINATED BY ''
      LINES TERMINATED BY '\r\n';
    
    INSERT INTO main_table SELECT SUBSTRING(countrystring,1,2), SUBSTRING(countrystring,3, 2), SUBSTRING(countrystring,5) from tmp_lines;
    

提交回复
热议问题