mySQL INSERT IGNORE不会“忽略". [英] mySQL INSERT IGNORE doesn't "ignore"

查看:376
本文介绍了mySQL INSERT IGNORE不会“忽略".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,INSERT IGNORE会插入一个新条目(如果尚不存在),如果存在,请忽略它.因此,我尝试这样做已有一段时间了,而且似乎没有用.这是我的尝试:

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)

推荐答案

INSERT语法:

没有IGNORE的情况下,与表中现有的UNIQUE索引或PRIMARY KEY值重复的行会导致重复键错误,并且该语句将中止.使用IGNORE,仍不会插入该行,但不会发出错误.

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.

您需要在md5sum上定义UNIQUE索引:

ALTER TABLE rss_feeds ADD UNIQUE INDEX (md5sum);

这篇关于mySQL INSERT IGNORE不会“忽略".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆