2015-10-14 81 views
0

我正在将一些数据从csv文件导入到MySQL并试图忽略重复的行。忽略重复的行

mysql_query("INSERT IGNORE INTO products (parent_product_url, child_product_url, swatch) VALUES ('".$row[0]."', '".$row[1]."', '".$row[2]."')"); 

我的csv文件。

polo.htm,red.htm,red.jpg 
polo.htm,green.htm,green.jpg 
round-neck.htm,green.htm,green.jpg 

现在,如果我运行下面的csv文件,因为他们已经在表中存在,它应该忽略前三行。它应该只插入第四行。

polo.htm,red.htm,red.jpg 
polo.htm,green.htm,green.jpg 
round-neck.htm,green.htm,green.jpg 
v-neck.htm,red.htm,red.jpg 
+0

什么是产品的主键,你有什么独特的键也? – zedfoxus

+0

ID是主键,它具有自动增量功能,我没有唯一键,但我认为我们可以将“child_product_url”视为唯一键。 – AZee

+0

好的,在这种情况下,MySQL的行为是正确的。如果该记录违反主键或唯一键,'insert ignore'将会引发警告并且不插入记录。由于这三个字段的组合并没有违反任何约束,所以MySQL乐于重复地插入这些数据。为了防止这种情况发生,您可以在parent_product_url上创建唯一的索引(如果这样做有意义)。 – zedfoxus

回答

0

我知道了这个答案的帮助下解决了 - >Insert query check if record exists - If not, Insert it

下面是我更新的查询

mysql_query("INSERT INTO products (parent_product_url, child_product_url, swatch) 
      SELECT * FROM (SELECT '".$row[0]."', '".$row[1]."', '".$row[2]."') AS tmp 
      WHERE NOT EXISTS (
      SELECT * FROM products WHERE parent_product_url='".$row[0]."' AND child_product_url='".$row[1]."' AND swatch='".$row[2]."' 
      );"); 
+0

谢谢。会做 :) – AZee

1

我喜欢on duplicate key update因为insert ignore忽略所有错误,而不仅仅是重复的错误。

无论你使用哪个,你的问题可能是缺乏唯一的约束/索引。

您不指定“重复”的含义。假设您指的是所有列:

create unique index unq_products_3 on products(parent_product_url, child_product_url, swatch); 

注意:根据存储引擎的不同,索引所用的键有最大长度。如果你的专栏太长,你可能需要考虑其他方法。

+0

是的,我的意思是所有的列,整个行。 – AZee

0

当您重新执行插入语句时会再次插入记录,因为插入未违反任何唯一或主键索引。因此MySQL没有什么可以忽略的。

create table products (
    parent_product_url varchar(100), 
    child_product_url varchar(100), 
    swatch varchar(100) 
); 

-- this will enter both records 
insert ignore into products values ('polo.htm', 'red.htm', 'red.jpg'); 
insert ignore into products values ('polo.htm', 'green.htm', 'green.jpg'); 

-- this will enter both records **AGAIN** 
insert ignore into products values ('polo.htm', 'red.htm', 'red.jpg'); 
insert ignore into products values ('polo.htm', 'green.htm', 'green.jpg'); 

现在让我们添加独特性parent_product_url,然后再试一次:

truncate table products; 
create unique index uk_products_parent_product_url on products(parent_product_url); 
insert ignore into products values ('polo.htm', 'red.htm', 'red.jpg'); 
insert ignore into products values ('polo.htm', 'green.htm', 'green.jpg'); 

这将只输入第一个记录。第二条记录将被忽略,并会引发警告。没有错误会被抛出。

如果你渴望拥有的3列的组合是唯一的,那么你可以这样做(这是戈登·利诺夫提及也......我只是增加更多的上下文):

alter table products drop key uk_products_parent_product_url; 
create unique index uk_products_parenturl_childurl_swatch on 
    products(parent_product_url, child_product_url, swatch); 
insert ignore into products values ('polo.htm', 'red.htm', 'red.jpg'); 
insert ignore into products values ('polo.htm', 'green.htm', 'green.jpg'); 

即使多次重复执行相同的2个插入语句,现在您仍会看到只插入两条记录。

https://dev.mysql.com/doc/refman/5.5/en/insert.html

如果使用忽略关键字,在执行 INSERT语句中出现的错误被忽略。例如,如果没有IGNORE, 重复表 中的现有UNIQUE索引或PRIMARY KEY值会导致重复键错误,并且语句会中止。使用 IGNORE,该行将被丢弃并且不会发生错误。忽略的错误可能会产生警告,尽管重复键错误不会。