更新 MySQL 表并忽略重复条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19516554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:11:36  来源:igfitidea点击:

Update MySQL table and ignore duplicate entries

mysql

提问by Vivek Buddhadev

I created a table that contains a UNIQUE 'mobile_no' like

我创建了一个包含 UNIQUE 'mobile_no' 之类的表

09727048248
9727048248
9824578564
9898998998

Then I am going to check whether or not the mobile number is valid, and if it's valid then I want to change it into the proper format like 919727048248.

然后我要检查手机号码是否有效,如果有效那么我想将其更改为正确的格式,例如919727048248。

For that I called update query like..

为此,我将更新查询称为..

update bccontacts 
set mobile_no='919727048248' 
where mobile_no=09727048248

The first time it ran successfully, but the second time it replied

第一次运行成功,但是第二次就回复了

ERROR 1062 (23000):Duplicate entry '919727048248' for key 'mobile_no'

错误 1062 (23000):重复条目 '919727048248' 键 'mobile_no'

Because there is already a unique key set for the 'mobile_no'.

因为已经为“mobile_no”设置了唯一键。

So is there any other query which will IGNORE DUPLICATE KEY ON UPDATE?

那么还有其他查询IGNORE DUPLICATE KEY ON UPDATE吗?

回答by Zaar Hai

Use UPDATE IGNORE:

使用UPDATE IGNORE

update IGNORE bccontacts 
set mobile_no='919727048248' 
where mobile_no=09727048248

More info here: http://dev.mysql.com/doc/refman/5.0/en/update.html

更多信息在这里:http: //dev.mysql.com/doc/refman/5.0/en/update.html

回答by R R

If you have declared the mobile number as the primary key in your table, then you can't have the same mobile number twice in your table. Have a look at the MySQL UPDATE documentation.

如果您已将手机号码声明为表中的主键,则您的表中不能有两次相同的手机号码。查看MySQL 更新文档