5

How can I use the ON DUPLICATE UPDATE with a multiple value INSERT?

INSERT INTO tbl_name 
  (key_id,field1,filed2) 
VALUES
  (1,2,3),
  (1,5,6),
  (1,8,9);
2
  • What is your intended outcome? I can only get MySQL to allow (1,5,6) ON DUPLICATE KEY UPDATE key_id=key_id+1, but on execution I only have one row in the table, values: 2, 2, 3. Commented Feb 8, 2010 at 20:16
  • well the SQL is an example but the expected outcome would be to run one query that would insert if new, update if duplicate for multiple values. so if key_id 1, 3, and 5 are already in the DB and I run INSERT for 2, 3, 4 and 6. 2,4 and 6 should insert and 3 should update since it was a duplicate Commented Feb 8, 2010 at 20:20

1 Answer 1

11

I can not try it right now, but can't you use this syntax

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
  ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);

from the manual?

Guess your syntax would then look like this:

INSERT INTO tbl_name
  (key_id,field1,filed2) 
VALUES
  (1,2,3),
  (1,5,6),
  (1,8,9)
ON DUPLICATE KEY
  UPDATE field1=VALUES(field1), field2=VALUES(field2);
Sign up to request clarification or add additional context in comments.

1 Comment

I must have missed this, thnx

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.