MySQLism: avoid UPDATE on duplicate

Last updated on
8 September 2016

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

MySQL has a nice SQL feature to update records when a duplicate key exist.

Example:

INSERT INTO single_login_history
SET uid = 'titi', 
session_id = 'sdklfsldkjflksdjfkls', 
date = 37487,
 ip = '192.243.0.1', 
browser = 'Geko', type = 'cookie' 
ON DUPLICATE KEY 
UPDATE date = 45034";

This feature is not part of SQL99. It will fail on any database.

Help improve this page

Page status: No known problems

You can: