2012-12-04 60 views
0

我做了新的触发器,它必须更新员工的工资。它从表格付款中获取2个值(现金和月),并将它们分成一列“how_much_to_pay”。无法更新触发器中的表

我看了一下题目: MySQL triggers cannot update rows in same table the trigger is assigned to. Suggested workaround?

但didin't帮我,因为他们只用1台,它们可以把“新”没有问题。

这里是我的触发器:

create trigger pay_out before insert on `payment_out` 
for each row 
then 
UPDATE `payment_out` o 
INNER JOIN payment p ON p.id_1 = o.id_1 AND o.id2 = p.id2 
SET o.`how_much_to_pay` = p.cash/p.months; 
end; 
$$ 

下面是表:

table payment_out 
id1 
id2 
how_much_to_pay 

table payment 
id1 
id2 
cash 
months 

和错误本身:

1442 - Can't update table payment_out in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

回答

1

如果你有一个一对一的关系之间的两个表,那么你可以在BEFORE INSERT触发器中使用这段代码 -

BEGIN 
    SELECT p.cash, p.months INTO @cash, @months 
    FROM payment p 
    WHERE p.id_1 = NEW.id_1 AND p.id2 = NEW.id2; 

    SET NEW.how_much_to_pay = @cash/@months; 
END 
+0

那么,这个工作,但“多少钱”是NULL,即使现金= 8000,月= 8。 – trinny

+0

如果p.cash或p.months之一是空值。尝试从触发器运行SELECT,将正确的值添加到WHERE条件。结果是什么? – Devart

+0

当我注意到它时,我将它更改为0.00。 – trinny