2

When using insert... on duplicate key update, what is the syntax to update multiple columns and not update some columns?

i wrote the following query:

INSERT INTO table (id,number,text) VALUES (1,2,3),(3,5,"i want this remain the previous value and not change"),(1,2,3)
  ON DUPLICATE KEY UPDATE id=VALUES(id), number=VALUES(number), text=VALUES(text);

how to not change some values and let them remain it's previous value?

1 Answer 1

1

Do it with IF:

INSERT INTO table (id,number,text) VALUES (1,2,3),(3,5,"ZZZ"),(1,2,3)
  ON DUPLICATE KEY UPDATE id=VALUES(id), number=VALUES(number), text=IF(VALUES(text)="ZZZ", text, VALUES(text));

-for multiple values use IN instead of =:

INSERT INTO table (id,number,text) VALUES (1,2,3),(3,5,"ZZZ"),(1,2,"YYY")
      ON DUPLICATE KEY UPDATE id=VALUES(id), number=VALUES(number), text=IF(VALUES(text) IN ("ZZZ", "YYY"), text, VALUES(text));
Sign up to request clarification or add additional context in comments.

1 Comment

can you say me how to use IN instead of =

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.