Community Documentation

MySQLism: avoid UPDATE on duplicate

Last updated April 13, 2010. Created by grub3 on April 13, 2010.
Log in to edit this page.

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.

Develop for Drupal

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here