php – Mysql CSV加载infile

我有一个包含9列的CSV文件,我有一个包含11列的MySQL表.

CSV文件如下所示:

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

和MySQL表看起来像:

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

我需要让脚本忽略两个错误的(但需要的)MySQL列.

导入中需要忽略的mysql列是:db_id& nice_date =)

这是我到目前为止:

$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)'
;

缺少的列将被赋予其DEFAULT值,否则您可以通过以下方式指定固定值:

$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'
;

转载注明原文:php – Mysql CSV加载infile - 代码日志