mysql inser ignore_mySQL INSERT IGNORE不会“忽略”。

bd96500e110b49cbb3cd949968f18be7.png

From my understanding INSERT IGNORE inserts a new entry if it doesn't already exists, if it does, ignore it. So I've been trying to do that for a while and it doesn't seem to be working. Here's my attempt:

insert insert ignore into rss_feeds (md5sum) values ("1234");

Query OK, 1 row affected (0.00 sec)

mysql> insert ignore into rss_feeds (md5sum) values ("1234");

Query OK, 1 row affected (0.00 sec)

mysql> insert ignore into rss_feeds (md5sum) values ("1234");

Query OK, 1 row affected (0.00 sec)

mysql> insert ignore into rss_feeds (md5sum) values ("1234");

Query OK, 1 row affected (0.00 sec)

mysql> select * from rss_feeds where md5sum="1234";

+------+--------+----------+---------+----------+--------+--------+---------+

| link | source | headline | updated | inserted | md5sum | itemid | emailed |

+------+--------+----------+---------+----------+--------+--------+---------+

| NULL | NULL | NULL | NULL | NULL | 1234 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL | 1234 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL | 1234 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL | 1234 | NULL | NULL |

+------+--------+----------+---------+----------+--------+--------+---------+

4 rows in set (0.00 sec)

解决方案

As documented under INSERT Syntax:

without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued.

You need to define a UNIQUE index on md5sum:

ALTER TABLE rss_feeds ADD UNIQUE INDEX (md5sum);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值