1

我有一个包含 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'
;
4

2 回答 2

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

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

$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'
;
于 2012-12-23T17:59:37.160 回答
0

看起来你可以使用LINES TERMINATED BY(也许与PHP_EOL)。
http://dev.mysql.com/doc/refman/5.0/en/load-data.html

引用手册页:

如果一行不包含所有字段,则其余列将设置为其默认值。

于 2012-12-23T17:45:29.810 回答