gpt4 book ai didi

mysql - 使用触发器计算金额..但无法获取价格值

转载 作者:行者123 更新时间:2023-11-29 19:45:19 25 4
gpt4 key购买 nike

我有下表:

Table Goods_has_Taker(order_id, good_id...)
Table Goods (good_id, price...)
Table OrderD (order_id, transaction_date, amount)

我正在使用触发器来计算金额。我的问题是它无法获得价格的值(value)。我的代码有什么问题吗?

CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`OrderD_BEFORE_INSERT`
BEFORE INSERT ON `OrderD`
FOR EACH ROW
BEGIN
DECLARE quantity INT;
DECLARE price FLOAT;

SET new.transaction_date = NOW(),
@quantity :=
(SELECT out_quantity
FROM Goods_has_Taker
WHERE Goods_has_Taker.order_id = new.order_id),
@price :=
(SELECT price
FROM Goods
INNER JOIN Goods_has_Taker
ON Goods.good_id = Goods_has_Taker.good_id
WHERE new.order_id = Goods_has_Taker.order_id
),
new.amount = @quantity * @price;
END

最佳答案

如果您将这行脚本 Where new.order_id = Goods_has_Taker.order_id 更改为 Where Goods_has_Taker.order_id = new.order_id

CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`OrderD_BEFORE_INSERT` 
BEFORE INSERT ON `OrderD` FOR EACH ROW
BEGIN
DECLARE quantity Int;
declare price FLoat;

set new.transaction_date=NOW(),
@quantity :=
(select out_quantity from Goods_has_Taker
where Goods_has_Taker.order_id=new.order_id),
@price :=
(select price from Goods
Inner Join Goods_has_Taker
On Goods.good_id=Goods_has_Taker.good_id
Where Goods_has_Taker.order_id = new.order_id
),
new.amount=@quantity*@price;
END

关于mysql - 使用触发器计算金额..但无法获取价格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41047655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com