[英]Mysql CSV load infile
I've got a CSV file with 9 columns and I have a MySQL table with 11 columns. 我有一个包含9列的CSV文件,我有一个包含11列的MySQL表。
The CSV file looks like: CSV文件如下所示:
col1, col2, col3, col4, col5, col6, col7, col8, col9
and the MySQL table looks like: 和MySQL表看起来像:
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. 我需要让脚本忽略两个错误的(但必需的)MySQL列。
The mysql columns that need to be ignored in the import are: db_id & nice_date =) 导入中需要忽略的mysql列是: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: 缺少的列将被赋予其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'
;
Looks like you can use LINES TERMINATED BY
(perhaps with PHP_EOL
). 看起来你可以使用
LINES TERMINATED BY
(可能使用PHP_EOL
)。
http://dev.mysql.com/doc/refman/5.0/en/load-data.html 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.
如果某行不包含所有字段,则其余列将设置为其默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.