简体   繁体   English

如果存在更新,否则插入CSV数据MySQL

[英]if exists update else insert csv data MySQL

I am populating a MySQL table with a csv file pulled from a third party source. 我正在使用从第三方来源提取的csv文件填充MySQL表。 Every day the csv is updated and I want to update rows in MySQL table if an occurrence of column a, b and c already exists, else insert the row. 每天更新csv,如果已经存在a,b和c列,我想更新MySQL表中的行,否则插入该行。 I used load data infile for the initial load but I want to update against a daily csv pull. 我使用加载数据infile进行初始加载,但我想针对每天的csv拉动进行更新。 I am familiar with INSERT...ON DUPLICATE, but not in the context of a csv import. 我熟悉INSERT ... ON DUPLICATE,但是在csv导入的上下文中却不熟悉。 Any advice on how to nest LOAD DATA LOCAL INFILE within INSERT...ON DUPLICATE a, b, c - or if that is even the best approach would be greatly appreciated. 任何有关如何将INSERT LOAD DATA LOCAL INFILE嵌套在INSERT ... ON DUPLICATE a,b,c中的建议-甚至是最好的方法,也将不胜感激。

LOAD DATA LOCAL INFILE 'C:\\Users\\nick\\Desktop\\folder\\file.csv' 
INTO TABLE db.tbl
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n' 
IGNORE 1 lines;     

Since you use LOAD DATA LOCAL INFILE, it is equivalent to specifying IGNORE: ie duplicates would be skipped. 由于您使用LOAD DATA LOCAL INFILE,因此等效于指定IGNORE:即,将跳过重复项。 But

If you specify REPLACE, input rows replace existing rows. 如果指定REPLACE,则输入行将替换现有行。 In other words, rows that have the same value for a primary key or unique index as an existing row. 换句话说,主键或唯一索引的值与现有行的值相同的行。

So you update-import could be 所以你更新导入可能是

LOAD DATA LOCAL INFILE 'C:\\Users\\nick\\Desktop\\folder\\file.csv' 
REPLACE
INTO TABLE db.tbl
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n' 
IGNORE 1 lines;

https://dev.mysql.com/doc/refman/5.6/en/load-data.html https://dev.mysql.com/doc/refman/5.6/zh-CN/load-data.html

If you need a more complicated merge-logic, you could import CSV to a temp table and then issue INSERT ... SELECT ... ON DUPLICATE KEY UPDATE 如果需要更复杂的合并逻辑,则可以将CSV导入到临时表中,然后发出INSERT ... SELECT ... ON DUPLICATE KEY UPDATE

I found that the best way to do this is to insert the file with the standard LOAD DATA LOCAL INFILE 我发现执行此操作的最佳方法是使用标准LOAD DATA LOCAL INFILE插入文件

LOAD DATA LOCAL INFILE 
INTO TABLE db.table
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n' 
IGNORE 1 lines;

And use the following to delete duplicates. 并使用以下命令删除重复项。 Note that the below command is comparing db.table to itself by defining it as both a and b. 请注意,以下命令通过将db.table定义为a和b来将其与自身进行比较。

delete a.* from db.table a, db.table b
where a.id > b.id
and a.field1 = b.field1
and a.field2 = b.field2
and a.field3 = b.field3; 

To use this method it is essential that the id field is an auto incremental primary key.The above command then deletes rows that contain duplication on field1 AND field2 AND field3. 要使用此方法,必须将id字段设置为自动增量主键,然后上述命令将删除在field1 AND field2 AND field3上包含重复项的行。 In this case it will delete the row with the higher of the two auto incremental ids, this works just as well if we were to use < instead of >. 在这种情况下,它将删除具有两个自动增量ID中较高者的行,如果我们使用<而不是>的话,这将同样有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM