gpt4 book ai didi

mySQL INSERT IGNORE 不是 "ignore"

转载 作者:行者123 更新时间:2023-11-29 01:14:08 26 4
gpt4 key购买 nike

根据我的理解,INSERT IGNORE 插入一个新条目,如果它不存在,如果存在,则忽略它。所以我已经尝试这样做了一段时间,但似乎没有用。这是我的尝试:

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 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.

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

ALTER TABLE rss_feeds ADD UNIQUE INDEX (md5sum);

关于mySQL INSERT IGNORE 不是 "ignore",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12429044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com