Phill Pafford
Phill Pafford

Reputation: 85318

mysql conditional insert on duplicate update - multiple records

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);

Upvotes: 5

Views: 2474

Answers (1)

Peter Lang
Peter Lang

Reputation: 55524

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);

Upvotes: 11

Related Questions