忽略INSERT上MySQL中的重复键错误 [英] Ignore duplicate key error in MySQL on INSERT

查看:71
本文介绍了忽略INSERT上MySQL中的重复键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表中的一行复制到另一个表中.那里的查询看起来像这样

I am trying to duplicate a row in my table, into another table. There query looks like this

INSERT INTO `quote_block_arc` (`id`,`quote_id`,`name`,`description`,`price`,`hours`,`days`,`total_hours`,`is_print`,`amends`) SELECT `id`,`quote_id`,`name`,`description`,`price`,`hours`,`days`,`total_hours`,`is_print`,`amends` FROM `quote_block` WHERE `quote_id` = '41'

但是,如果没有保存该行的重复键,是否可以忽略该警告并运行查询?

However, it failing saving there is a duplicate key for this row, is there a way to ignore that warning and run the query?

推荐答案

发生此错误是因为您已将表的列之一指定为UNIQUE.您不能在此列中有2行具有相同的值.如果要替换现有行,请使用REPLACE而不是INSERT.如果您确实希望该列的行包含相同的值,请从该列中删除UNIQUE修饰符.

This error occurs because you have specified one of the columns of your table to be UNIQUE. You cannot have 2 rows with the same value for this column. If you want to replace the existing row instead, use REPLACE instead of INSERT. If you really want rows containing the same value for the column, remove the UNIQUE modifier from that column.

使用其他一些答案中所述的INSERT IGNORE可以防止发出错误,但不会更新表.

Using INSERT IGNORE as described in some of the other answers will prevent the error being issued, but will not update the table.

这篇关于忽略INSERT上MySQL中的重复键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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