如果存在则更新 MySQL [英] IF Exists Then Update MySQL
问题描述
我试图更新一行,如果它存在,如果它不存在,那么我想在表中执行插入操作.图片.id &picture.picturepath 是一个唯一的键.我看过一些例子,但我不确定我做错了什么.
I am trying to update a row if its exists, if it does not exist then I want to perform an insert into the table. The picture.id & picture.picturepath are a unique key. I have looked at some examples but I am not sure What I am doing wrong.
我遇到了重复的密钥更新,不确定这是否与我要实现的目标有关.
I have come across on duplicate key update, not sure if this is relevant to what I am trying to achieve.
Error message: 1064- You have an error in your SQL syntax near 'UPDATE picture SET picture.picturecontent = ipicturecontent WHERE picture.id at line 5"
IF EXISTS( SELECT * FROM picture WHERE picture.id = ipictureid
AND picture.picturepath = ipicturepath) THEN
UPDATE picture
SET picture.picturecontent = ipicturecontent
WHERE picture.id = ipictureid
AND picture.picturepath = ipicturepath
ELSE
INSERT INTO picture (picture.id, picture.picturecontent,picture.picturepath) VALUES (ipictureid, ipicturecontent, ipicturepath)
推荐答案
https://stackoverflow.com/a/10095812/1287480 <- 信用到期的信用
https://stackoverflow.com/a/10095812/1287480 <- Credit where credit is due
插入模型(col1、col2、col3)值 ('foo', 'bar', 'alpha')重复密钥更新 col3='alpha';
INSERT INTO models (col1, col2, col3) VALUES ('foo', 'bar', 'alpha') ON DUPLICATE KEY UPDATE col3='alpha';
这篇关于如果存在则更新 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!