Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql conditional insert on duplicate update - multiple records

Tags:

sql

mysql

insert

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);
like image 435
Phill Pafford Avatar asked Feb 08 '10 19:02

Phill Pafford


1 Answers

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);
like image 140
Peter Lang Avatar answered Oct 25 '22 15:10

Peter Lang