Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CSV到Mysql导入脚本以匹配字段_Php_Mysql_Csv - Fatal编程技术网

Php CSV到Mysql导入脚本以匹配字段

Php CSV到Mysql导入脚本以匹配字段,php,mysql,csv,Php,Mysql,Csv,是否有这样一个脚本,或者可以将CSV中的数据导入mysql,使其与第一个字段匹配,从而只更新行中缺少的字段或值,而不是覆盖数据?希望我已经解释清楚了 例如: 1,john,doe,programmer 2,jane,doe,accountant 3,judy,doe,manager 1,john,doe,cto 假设第一个字段是一个ID,我希望脚本在记录不存在时插入,但在ID已经存在时更新,因此John Doe将首先作为程序员插入,然后更新为CTO。尝试以下内容可能对您有所帮助 祝您好运从,在

是否有这样一个脚本,或者可以将CSV中的数据导入mysql,使其与第一个字段匹配,从而只更新行中缺少的字段或值,而不是覆盖数据?希望我已经解释清楚了

例如:

1,john,doe,programmer
2,jane,doe,accountant
3,judy,doe,manager
1,john,doe,cto

假设第一个字段是一个ID,我希望脚本在记录不存在时插入,但在ID已经存在时更新,因此John Doe将首先作为程序员插入,然后更新为CTO。

尝试以下内容可能对您有所帮助

祝您好运

从,在:

REPLACE和IGNORE关键字控制对以下输入行的处理: 在唯一键值上复制现有行:

If you specify REPLACE, input rows replace existing rows.
In other words, rows that have the same value for a primary key or unique index
as an existing row. See Section 12.2.7, “REPLACE Syntax”.

If you specify IGNORE, input rows that duplicate an existing row
on a unique key value are skipped. If you do not specify either
option, the behavior depends on whether the LOCAL keyword is
specified. Without LOCAL, an error occurs when a duplicate key value
is found, and the rest of the text file is ignored. With LOCAL, the
default behavior is the same as if IGNORE is specified; this is
because the server has no way to stop transmission of the file in the
middle of the operation.
因此,我相信如果您将存储名称的字段设置为主键或唯一索引,您应该能够:

LOAD DATA LOCAL INFILE 'file.csv'
REPLACE INTO TABLE yourtable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(id, name, surname, etc);

更新:

刚刚找到更多关于该主题的资源: