Mysql CSV load infile

前端 未结 2 857
广开言路
广开言路 2021-01-01 07:55

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

The CSV file looks like:

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


        
相关标签:
2条回答
  • 2021-01-01 08:25

    Looks like you can use LINES TERMINATED BY (perhaps with PHP_EOL).
    http://dev.mysql.com/doc/refman/5.0/en/load-data.html

    To quote the manual page:

    If a line does not contain all fields, the rest of the columns are set to their default values.

    0 讨论(0)
  • 2021-01-01 08:36
    $sql = 'LOAD DATA LOCAL INFILE "../csvtemp/test.csv" 
            INTO TABLE sample 
                FIELDS TERMINATED BY "," 
                OPTIONALLY ENCLOSED BY """" 
                IGNORE 1 LINES
                (col1, col2, col3, col4, col5, col6, col7, col8, col9)'
    ;
    

    The missing columns will be given their DEFAULT values, or else you can specify fixed values this way:

    $sql = 'LOAD DATA LOCAL INFILE "../csvtemp/test.csv" 
            INTO TABLE sample 
                FIELDS TERMINATED BY "," 
                OPTIONALLY ENCLOSED BY """" 
                IGNORE 1 LINES
                (col1, col2, col3, col4, col5, col6, col7, col8, col9)'
                SET col10 = 'abc', col11 = 'xyz'
    ;
    
    0 讨论(0)
提交回复
热议问题