Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update MySQL table and ignore duplicate entries

Tags:

mysql

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

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.

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'

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

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

like image 236
Vivek Buddhadev Avatar asked Oct 22 '13 11:10

Vivek Buddhadev


People also ask

How do I ignore duplicate entries in MySQL?

Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.

How do you prevent duplicate records in SQL update query?

Using COUNT(*) = 0. To avoid duplicates, the COUNT or records returned by the subquery above should be zero. Note: You can design any query visually in a diagram using the Query Builder feature of dbForge Studio for SQL Server.


1 Answers

Use 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

like image 147
Zaar Hai Avatar answered Sep 28 '22 07:09

Zaar Hai