MySQL-When子句
我试图使用MySQL创建一个触发器,但我遇到了一个错误。限制条件是:用户不能对他或她正在销售的商品出价MySQL-When子句,mysql,sql,Mysql,Sql,我试图使用MySQL创建一个触发器,但我遇到了一个错误。限制条件是:用户不能对他或她正在销售的商品出价 Create Trigger before_insert_bid Before Insert on Bid for each row when exists( Select * From Item Where Item.UserID = new.UserID AND Item.ItemID
Create Trigger before_insert_bid
Before Insert on Bid
for each row
when exists(
Select *
From Item
Where Item.UserID = new.UserID
AND Item.ItemID = new.ItemID
)
begin
Call 'You cannot bid on your own item';
end;
在我的一生中,我无法弄清楚错误是什么,以及我将如何修复它,在过去的一个小时里,我一直在尝试调试它,是我错误地使用了when子句,还是我必须使用其他东西
谢谢你的帮助 类似于:
...
for each row
IF EXISTS(
Select *
From Item
Where Item.UserID = new.UserID
AND Item.ItemID = new.ItemID
) then
begin
Call `You cannot bid on your own item`;
end;
end if;
在MySQL中,触发器如下所示:
delimiter $$
create Trigger before_insert_bid before Insert on Bid for each row
begin
if (exists (Select 1 from Item i Where i.UserID = new.UserID and i.ItemID = new.ItemID))
then
signal sqlstate '45000' set message_text = 'You cannot bid on your own item';
end if;
end;$$
delimiter ;
自MySQL 5.5以来,
信号
是从触发器返回错误的首选方法。在体内使用if
。具体如何?抱歉,我是新手。如果存在,请尝试(…
这就是我所做的:在插入前创建触发器\u在插入后对每一行进行出价如果Item.UserID=new.UserID和Item.ItemID=new.ItemID,那么调用'Error';end if;end;但是它仍然不起作用我尝试了类似的方法,但是它在call语句中抛出了一个错误,表示语法不正确,do您知道为什么吗?您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以了解第11行“Error1:您不能对自己的商品出价”附近使用的正确语法。您是否在call语句中使用了”
引号?是的,我确实使用了。我不应该调用“Error1:您不能对自己的商品出价”自己的项目“;@vampyfreak-试试引号,我对调用语句的回答是什么。注意,它们不是”
谢谢你的帮助,但是我们不能使用信号,因为我们学校的mysql不是最新的。如果我想为同一个触发器添加另一个约束,我该怎么做?@vampyfreak…如果你想添加另一个代码>。