在重复键忽略?

2021-11-20 00:00:00 mysql

我正在尝试完成此查询;我的标签字段设置为 UNIQUE,我只是希望数据库忽略任何重复的标签.

I'm trying to finish this query; my tag field is set to UNIQUE and I simply want the database to ignore any duplicate tag.

INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c')
ON DUPLICATE KEY IGNORE '*the offending tag and carry on*'

甚至这也是可以接受的

INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c')
ON DUPLICATE KEY UPDATE '*the offending tag and carry on*'

推荐答案

建议不要使用 INSERT IGNORE,因为它会忽略所有错误(即它是一个草率的全局忽略).相反,由于在您的示例中 tag 是唯一键,请使用:

Would suggest NOT using INSERT IGNORE as it ignores ALL errors (ie its a sloppy global ignore). Instead, since in your example tag is the unique key, use:

INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c')
ON DUPLICATE KEY UPDATE tag=tag;

重复键产生:

查询成功,0 行受影响(0.07 秒)

Query OK, 0 rows affected (0.07 sec)

相关文章