52

我创建了一个包含唯一“mobile_no”的表

09727048248
9727048248
9824578564
9898998998

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

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

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

第一次运行成功,第二次又回复了

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

因为“mobile_no”已经有一个唯一的密钥集。

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

4

2 回答 2

114

使用UPDATE IGNORE

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

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

于 2014-07-21T16:15:57.213 回答
0

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.

于 2013-10-22T11:36:06.283 回答