我需要向DB插入信息,但是如果信息" user_id"已存在更新,如下所示:(问题是user_id不是KEY)
INSERT INTO access_token
(id,access_token,user_id)
VALUES
(1062371433,106237146,10623714)
ON DUPLICATE user_id <=== receive error in this line
UPDATE
access_token = VALUES(access_token)
但在第5行收到错误,
如何解决?
感谢,
答案 0 :(得分:0)
set @founded_id=(select user_id from access_token where user_id='10623714');
if(@founded_id='10623714')then
update access_token set ............
else
INSERT INTO access_token
(id,access_token,user_id)
VALUES
(1062371433,106237146,10623714)
答案 1 :(得分:0)
你可以用这个:
INSERT ON DUPLICATE KEY UPDATE
INSERT INTO access_token
(id,access_token,user_id)
VALUES
(1062371433,106237146,10623714)
ON DUPLICATE KEY
UPDATE
access_token = VALUES(access_token);