Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

ON DUPLICATE KEY UPDATE für Multi-Row-Inserts in MySQL

Ab MySQL 8.0.19 können Sie beim Multi-Row-Insert einen Alias für die eingehende Zeile verwenden:

INSERT INTO beautiful (name, age)
VALUES ('Helen', 24), ('Katrina', 21)
AS new
ON DUPLICATE KEY UPDATE age = new.age;

In älteren Versionen wird VALUES() verwendet:

INSERT INTO beautiful (name, age)
VALUES ('Helen', 24), ('Katrina', 21)
ON DUPLICATE KEY UPDATE age = VALUES(age);

Die Alias-Variante ist klarer und zukunftssicherer.