Thursday 25 June 2020

Insert into a MySQL table or update if exists

https://stackoverflow.com/questions/4205181/insert-into-a-mysql-table-or-update-if-exists

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
 
 
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),
    ...
 

 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.