Jump to content

on duplicate key update


facarroll

Recommended Posts

I would like to be able to do the following pseudo-code using ON DUPLICATE KEY UPDATE, but I cannot find one unique key to nominate. Can I use ON DUPLICATE KEY UPDATE with the use of a combination of non-unique keys such as $id and $address which become unique upon combination? I use MySql v5.5.8

 

if ($id exists in table AND $address exists in table) { UPDATE field1=value1, field2=value2  where id=$id AND address=$address } else { insert into table field1=$value1, field2=$value2 }

 

Thanks.

Link to comment
Share on other sites

You can add a multi-key which contains two columns.

 

Name      Age
-----------------
Ryan       12
Ryan       13
Ryan       12

 

So using the above, with a unique key on "Name" and "Age" (to make one unique key),

Ryan 12 and Ryan 13 are two different things thus an "on duplicate key update" won't occur.

But...

Ryan 12 and Ryan 12 are the same thing so thus "on duplicate key update" will occur, when the second one get added with an insert.

 

When storing the keys, MySql visually stores them like so (I don't know how MySQL physically stores them):

Ryan-12

Ryan-13

Ryan-45

Ryan-38

 

and so on where Ryan is from column one and the number is from column two.

Link to comment
Share on other sites

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.