sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo actualizo si existe, inserto si no (AKA upsert o merge) en MySQL?

Utilice INSERT ... ON DUPLICATE KEY UPDATE . Por ejemplo:

INSERT INTO `usage`
(`thing_id`, `times_used`, `first_time_used`)
VALUES
(4815162342, 1, NOW())
ON DUPLICATE KEY UPDATE
`times_used` = `times_used` + 1