【问题标题】:trying to create a trigger that updates the quantity of one table from another table试图创建一个触发器,从另一个表更新一个表的数量
【发布时间】:2015-10-19 16:27:23
【问题描述】:

我有两张用于销售产品的表格。一个是实际产品的表格,另一个是订购商品的表格。

ordered item 表记录了 itemId 和售出的数量。

我想创建一个触发器,从ordered item 表中获取数据并更新product 表数量,该列称为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$$

我的错误信息:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 '(quantityInStock) 附近使用的正确语法 在第 5 行设置 quantityInStock = quantityInStock - orderItem.quantity w'

【问题讨论】:

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

标签: mysql sql mysql-error-1064


【解决方案1】:

检查您的更新语法。

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

您可以分别使用old.column_namenew.column_name 引用上一个/新行值。

需要考虑的一些事项:

如何防止数量为负数

【讨论】:

  • 谢谢。我从更新中删除了 (quantityInStock) 并添加了“新”。它奏效了。
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 2011-02-10
  • 2017-05-17
  • 1970-01-01
  • 2020-08-23
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
相关资源
最近更新 更多