2015-10-19 53 views
0

我有两个表正在出售的产品。一个是实际产品的表格,另一个是订购商品的表格。试图创建一个触发器,从另一个表更新一个表的数量

订购商品表记录itemId和销售数量。

我想创建一个触发器,它从订购商品表中获取数据,并更新该商品表的数量,该商品表的数量称为quantityInStock。

这是我写的触发器,但我不断收到一个错误:

delimiter $$ 

create trigger stockUpdate 
after insert on orderItem 
for each row 
begin 
    update item(quantityInStock) 
    set quantityInStock = quantityInStock - orderItem.quantity  
    where itemId = orderItem.itemId; 
end$$ 

我的错误信息:

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 '(quantityInStock) set quantityInStock = quantityInStock - orderItem.quantity w' at line 5

+0

@Quanlong谢谢。仍然在学习如何正确使用它。 –

回答

0

检查更新的语法。

update table_name 
set column_name = ... 
where column_name = ...; 

您可以old.column_namenew.column_name分别参考前/新行值。

有些事情要考虑:

如何预防量变为负值

+0

谢谢。我从更新后的“新”中删除了(quantityInStock)。它的工作。 –

相关问题