关于多个唯一约束的重复键更新

时间:2012-12-15 10:04:15

标签: mysql

我有一个在两列上有唯一约束的表。我正在尝试使用重复密钥更新,但收到错误...

...创建表格

CREATE TABLE IF NOT EXISTS `requests` (
  `userFrom` int(11) NOT NULL,
  `userTo` int(11) NOT NULL,
  `dateSent` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `userFrom` (`userFrom`,`userTo`)
)

...添加一些数据

INSERT INTO requests (userFrom, userTo) VALUES (1, 2)

...然后失败(当传递userFrom为1且userTo为2时

INSERT INTO requests (userFrom, userTo) VALUES (:userFrom, :userTo) ON DUPLICATE KEY UPDATE set dateSent = NOW()

错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set dateSent = NOW()' at line 1

1 个答案:

答案 0 :(得分:3)

我认为此处不需要set关键字,请尝试此操作

INSERT INTO requests (userFrom, userTo) VALUES (:userFrom, :userTo) ON DUPLICATE KEY UPDATE dateSent = NOW()