Insert into a MySQL table or update if exists Insert into a MySQL table or update if exists mysql mysql

Insert into a MySQL table or update if exists


Use INSERT ... ON DUPLICATE KEY UPDATE

QUERY:

INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE    name="A", age=19


Check out REPLACE

http://dev.mysql.com/doc/refman/5.0/en/replace.html

REPLACE into table (id, name, age) values(1, "A", 19)


When using batch insert use the following syntax:

INSERT INTO TABLE (id, name, age) VALUES (1, "A", 19), (2, "B", 17), (3, "C", 22)ON DUPLICATE KEY UPDATE    name = VALUES (name),    ...