我是触发器的新手,但已对此进行了编码
DELIMITER $$
CREATE TRIGGER stockupdate
AFTER INSERT ON inventory.orderdetails
FOR EACH ROW
BEGIN
UPDATE inventory.stockitem s
INNER JOIN inventory.OrderDetails d ON s.ID = d.Item
INNER JOIN inventory.orders o ON ebayOrderNumber = d.OrderNumber
SET s.`Sold Date` = o.`Order Date`, s.EbayOrderNumber = o.ebayOrderNumber, s.`Sale Price` = d.Price
WHERE s.ID = d.Item;
END$$
DELIMITER ;
目前它没有按预期更新表stockitem。有什么明显表明我做错了吗?
非常感谢您的光临。
答案 0 :(得分:0)
那是因为你的联接会从oderDetails和oders表返回所有或大多数记录,而你并没有压制sql_safe_updates
安全功能。
您肯定在o ON ??.ebayOrderNumber = d.OrderNumber
中缺少表别名。此外,WHERE s.ID = d.Item;
是多余的,因为您的表连接已经这样做了。您的Where条件应更明确,并告知要更新的库存项目。
DELIMITER $$
CREATE TRIGGER stockupdate AFTER INSERT ON inventory.orderdetails FOR EACH ROW
BEGIN
UPDATE
(inventory.stockitem s
INNER JOIN inventory.OrderDetails d ON s.ID = d.Item)
INNER JOIN inventory.orders o ON o.ebayOrderNumber = d.OrderNumber
SET
s.`Sold Date` = o.`Order Date`, -- update the stock
s.EbayOrderNumber = o.ebayOrderNumber,
s.`Sale Price` = d.Price
WHERE
d.orederNumber = NEW.OrderNumber, -- From this order
d.Item = NEW.item; -- and this item id
END$$
DELIMITER ;
话虽如此,我不明白您为什么要以售价更新股票表? orderDetails
已经包含了适合您的产品,如果您拥有多个库存数量,该怎么办?即100台戴尔笔记本电脑。当然,您的库存项目中每台戴尔笔记本电脑没有100条记录?并以售价更新每条记录?无论如何,我把它留给你......