triggers to multiply 'trigger-based columns' in mysql

最后都变了- 提交于 2019-12-13 04:30:42

问题


I have asked this question before and I want to elaborate it.

Triggers to connect multiple tables

I have the following trigger (kind of like the one I asked before):

CREATE TRIGGER trigger_test1
BEFORE INSERT ON test1 
FOR EACH ROW 
  SET NEW.OriginIndex = (SELECT index1 FROM cities WHERE city = NEW.Origin),
    NEW.DestinationIndex = (SELECT index1 FROM cities WHERE city =NEW.Destination);

This part works well. Now I want to multiply OriginIndex and DestinationIndex and store it in another column (let's say 'Multiplication').

I did this:

CREATE TRIGGER trigger_test1
BEFORE INSERT ON test1 
FOR EACH ROW 
  SET NEW.OriginIndex = (SELECT index1 FROM cities WHERE city = NEW.Origin),
    NEW.DestinationIndex = (SELECT index1 FROM cities WHERE city =NEW.Destination),
        NEW.Multiplication = (SELECT NEW.OriginIndex*NEW.DestinationIndex);

This gives an error 'This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table''.

How am I supposed to approach this problem?


回答1:


I think you can use

DELIMITER //
CREATE TRIGGER trigger_test1
BEFORE INSERT ON test1 
BEGIN
FOR EACH ROW 
SET NEW.OriginIndex = (SELECT index1 FROM cities WHERE city = NEW.Origin);
SET NEW.DestinationIndex = (SELECT index1 FROM cities WHERE city =NEW.Destination);
SET NEW.Multiplication = (NEW.OriginIndex*NEW.DestinationIndex);
END//

I hope this help.



来源:https://stackoverflow.com/questions/18137792/triggers-to-multiply-trigger-based-columns-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!