1

I would like to know if it is possible to use REPLACE MySQL statement in an InnoDB table to see if the key in the table already exists, if it does then increment the value by certain number (for instance 50), else insert a new record with with a certain value (50). Something like this:

 REPLACE INTO test(id, NumValue) VALUES (1, NumValue + 50) 

This statement doesn't work. It inserts/updates to NULL value. If there is a way to do this using REPLACE statement what is the correct syntax? I know I could do it in couple steps, first using SELECT statement to find out if the record already exists, if it does then do an UPDATE, if it doesn't, then do an INSERT. But I was hoping it could be achieved by using just one step, using REPLACE. And also, I want to avoid using sub-queries.

user2395238
  • 850
  • 1
  • 9
  • 20
  • possible duplicate of [Insert to table or update if exists (MySQL)](http://stackoverflow.com/questions/4205181/insert-to-table-or-update-if-exists-mysql) – Piotr Olaszewski Feb 17 '15 at 07:49
  • it's not duplicate. I am not asking about REPLACE function. I want to use REPLACE to increment existing value if it exists, or insert new if it does not. That question only talks about inserting/updating, not incrementing – user2395238 Feb 17 '15 at 07:50
  • Man but you even look at those links... http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html look here - it's first example. – Piotr Olaszewski Feb 17 '15 at 07:54
  • Yep, that answers my question. Still don't think this is a duplicate though. – user2395238 Feb 17 '15 at 07:55
  • Of course it is. Even subject of this topic is insert or **update if exists**. – Piotr Olaszewski Feb 17 '15 at 07:56
  • I just read the description, it actually might not work on InnoDb table – user2395238 Feb 17 '15 at 07:59

0 Answers0