尝试创建触发器时出现语法错误。我不是MySQL专家,但是根据我在stackoverflow上找到的信息设法创建了以下代码。触发的目的是检查销售行是否与拍卖品(而不是现在购买的物品)相关,然后是否计算出售价的10%的溢价(将溢价金额插入溢价栏中) 。 “listing_id”是列表表和销售列表表之间的链接。
DELIMITER $$
CREATE TRIGGER add_premium
AFTER INSERT
ON sales_listings FOR EACH ROW
BEGIN
SET @listing_type := (SELECT listing_type
FROM listings
WHERE listings.listing_id = NEW.listing_id);
IF @listing_type = 'auction' THEN
NEW.premium = NEW.price * 0.1;
ELSE
NEW.premium = 0;
END IF;
END$$
DELIMITER;
这是语法错误:
#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 '.premium = NEW.price * 0.1;
ELSE
NEW.premium = 0;
END IF;
' at line 9
我究竟做错了什么?
最佳答案
分配之前需要添加SET
。
IF @listing_type = 'auction' THEN
SET NEW.premium = NEW.price * 0.1;
ELSE
SET NEW.premium = 0;
END IF;