简体   繁体   中英

Mysql CSV load infile

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, col9

and the MySQL table looks like:

col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11

I need to get the script to ignore the two erroneous (but required) MySQL columns.

The mysql columns that need to be ignored in the import are: db_id & nice_date =)

This is what I have so far:

$sql = 'LOAD DATA LOCAL INFILE "../csvtemp/test.csv" 
        INTO TABLE sample 
            FIELDS TERMINATED BY "," 
            OPTIONALLY ENCLOSED BY """" 
            IGNORE 1 LINES'
;
$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'
;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM