March 3, 2012

ON DUPLICATE KEY UPDATE refuses to update

Question by Wade Urry

Having some troubles with ON DUPLICATE KEY UPDATE in MySQL. Below is the query im trying to run.

INSERT INTO `Overall` ( `rsn` , `starting_xp` , `starting_lvl` ) VALUES ( 'iWader' , '195843626' , '2281' ) ON DUPLICATE KEY UPDATE `current_xp` = '195843626' AND `current_lvl` = '2281'
  • It inserts fine, but when there is a duplicate it doesnt update, and doesnt throw any errors.
  • Running the query through PMA returns no error and doesnt update
  • Removing the ON DUPLICATE KEY UPDATE section returns a duplicate key error

This is the structure of my table

CREATE TABLE IF NOT EXISTS `overall` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rsn` varchar(12) NOT NULL,
  `starting_xp` int(10) unsigned NOT NULL,
  `starting_lvl` int(10) unsigned NOT NULL,
  `current_xp` int(10) unsigned NOT NULL,
  `current_lvl` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `rsn` (`rsn`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Answer by Starx

After ON DUPLICATE KEY UPDATE you should not need to use and with the fields, use , instead.

ON DUPLICATE KEY UPDATE `current_xp` = '195843626', `current_lvl` = '2281'
...

Please fill the form - I will response as fast as I can!