Mike Q
Mike Q

Reputation: 7327

Update mysql row if value different or create new row not added yet

For some reason this is not coming to me, and I do not see this specific question answered where there are two conditions. I want to update my_table for the following two conditions:

  1. If the Primary key vin is the same and the price is different. - or -
  2. If there is no vin then just create a new row

Example of before and after two example updates:

enter image description here

In this exable vin 12345 exist so just the price is update as it's different, in the other example vin 55555 wasn't in the table so it's created.

Upvotes: 0

Views: 32

Answers (1)

agim
agim

Reputation: 1841

MySql supports a conditional insert/update - check https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

INSERT INTO my_table (vin,price) VALUES (12345, 7777)
    ON DUPLICATE KEY UPDATE price=7777;

Upvotes: 2

Related Questions