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

...创建表

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)


...然后失败(当传递了1的userFrom和2的userTo时

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

最佳答案

我认为这里不需要set关键字,请尝试

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

07-24 17:09