更新/插入

时间:2013-10-15 09:38:16

标签: mysql

当我使用这个mysql语句时,我总是会得到一个新行而不是更新?

INSERT INTO buyclick SET counter=1, link_id=3 
ON DUPLICATE KEY UPDATE
  id = LAST_INSERT_ID(id),
  counter = counter + 1,
  link_id = 3 

表格结构:

CREATE TABLE `buyclick` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `link_id` int(11) DEFAULT NULL,
  `counter` int(11) DEFAULT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `clickeditem` (`link_id`),
  KEY `count` (`counter`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

1 个答案:

答案 0 :(得分:0)

您还应插入id,以便触发更新。例如,如果您已经拥有ID为10的记录,请执行以下操作:

INSERT INTO buyclick SET id=10, counter=1, link_id=3 
ON DUPLICATE KEY UPDATE
  counter = counter + 1,
  link_id = 3

假设您的表格中有一个主键id