我目前正在开发一个允许用户将项目添加到集合的切换系统。然后,他们可以删除从数据库中删除行的项目。这很容易使用2个查询完成,但我想用存储过程替换它,以节省必须使用2个查询执行此操作。我想要做的是检查一行是否存在,是否删除它,如果没有,则插入它。
我正在使用phpmyadmin,我正在尝试创建例程。我添加了2(itemid,使用设置为“IN”的参数
IF (SELECT 1 = 1 FROM useritems WHERE ItemID = itemid AND UserID = userid) THEN
BEGIN
DELETE FROM useritems WHERE ItemID = itemid AND UserID = userid
END;
ELSE
BEGIN
INSERT INTO useritems (ItemID, UserID) VALUES(itemid, userid)
END;
END IF;
当我尝试提交时,我收到以下错误
MySQL said: #1064 - 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 'END;
ELSE
BEGIN
INSERT INTO useritems (ItemID, UserID) VALUES(ite' at line 4
答案 0 :(得分:0)
<iframe id="topf" src="stacktop.php"></iframe>
<script>
function reload_top() {
var a = document.getElementById('topf');
a.src = "http://www.aapastech.com";
}
</script>
答案 1 :(得分:0)
这是IF
中MySQL
语句的正确语法:
IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list] ... [ELSE statement_list] END IF
所以修改你的代码是这样的:
IF EXISTS (SELECT 1 FROM useritems WHERE ItemID = itemid AND UserID = userid) THEN
DELETE FROM useritems WHERE ItemID = itemid AND UserID = userid;
ELSE
INSERT INTO useritems (ItemID, UserID) VALUES(itemid, userid);
END IF;