Jump to content

[SOLVED] Multiple insert query, how to ignore duplicate messages.


Recommended Posts

Hello all,

 

I added a composite key to a table that's lacking a primary key. Now i have an automated PHP script that utilizes the multi-insert query to populate this table on a daily basis. The issue I'm having is that when there's a duplicate value, i want to be able to add the ones that it can but ignore the ones that already exist.

 

Is there a way to get this result using PHP/MySQL?

Change INSERT INTO to REPLACE INTO?? Unless I'm misunderstanding what you mean.

 

Thanks for your response. In my situation, this worked. But in the MySQL certification study guide, i found some more info on the subject:

 

If you don't indicate explicitly how to handle a duplicate, MySQL aborts the statement with an error and discards the new record. This is the default behavior. (For multiple-record INSERT statements, treatment of records inserted before a record that causes a duplicate-key violation is dependent on the storage engine. For MyISAM, the records are inserted. For InnoDB, the entire statement fails and no records are inserted.)

 

You can tell MySQL to ignore the new record without producing an error. To do this, modify the statement so that it begins with INSERT IGNORE rather than with INSERT. If the record does not duplicate a unique key value, MySQL inserts it as usual. If the record does contain a duplicate key, MySQL ignores it. Client programs that terminate on statement errors will abort with INSERT but not with INSERT IGNORE.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.