1

I have table SomeTable with 3 columns A, B, C and a unique on A and B

From client side I am passing some values to insert or update C if A,B already exist. I want to know if the server has done update or insert for each value and if it is an update I want the old value of C....

I am currently doing this with

While values to insert {
   Insert A,B,C
   if success memorizing the value continue the while
   else if the sql error is "duplicate key" {
       select old value of C for A,B
       update C for A,B...
   }    
}

Which means 3 calls to MySQL....

Is there a way to do this with a trigger, a procedure or a "magic" query ?

  • Do you want to [on duplicate key update](http://stackoverflow.com/a/1361368/112968) or `REPLACE`? – knittl Sep 22 '13 at 18:01

1 Answers1

1

You can do this with two queries:

  1. Attempt to SELECT the old value. If it exists, you have it to return after step 2
  2. UPDATE or INSERT as needed
Explosion Pills
  • 176,581
  • 46
  • 285
  • 363