gpt4 book ai didi

php - Mysql CSV 加载文件

转载 作者:行者123 更新时间:2023-11-29 04:49:05 26 4
gpt4 key购买 nike

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

缺失的列将被赋予默认值,或者您可以通过这种方式指定固定值:

$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 加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14012883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com