MySQL ON DUPLICATE KEY with VALUES()
MySQL ON DUPLICATE KEY with VALUES() is a SQL statement in the MySQL Specific category. MySQL: VALUES(column) in ON DUPLICATE KEY UPDATE references the value that would have been inserted. The syntax is INSERT INTO t (id, count) VALUES (1, 1) ON DUPLICATE KEY UPDATE count = VALUES(count) + 1;. It returns upsert with values. A typical example: INSERT INTO page_views (page, views) VALUES ('/home', 1) ON DUPLICATE KEY UPDATE views = views + VALUES(views); -- VALUES(views) = 1 (the proposed insert value) -- = views = views + 1 -- This is MySQL-specific syntax -- In MySQL 8.0.20+, the VALUES() function for this purpose is deprecated -- Use alias-based syntax instead: INSERT INTO page_views (page, views) VALUES ('/home', 1) AS new ON DUPLICATE KEY UPDATE views =… A close relative is AUTO_INCREMENT, which mySQL auto-generates sequential integer values for this column. Commonly used with PRIMARY KEY. A close relative is ENGINE, which specifies the MySQL storage engine. InnoDB (default) supports transactions and foreign keys. MyISAM is faster for read-heavy.