开发者

Sum two columns with a trigger

开发者 https://www.devze.com 2023-04-02 09:32 出处:网络
I have a table (myTable) with 3 columns [s1, s2, sum] And I want to add a trigger that automatically updates sum with s1+s2 on each update. Thats my code but it doesn\'t work. What I\'m doing wrong?

I have a table (myTable) with 3 columns [s1, s2, sum]

And I want to add a trigger that automatically updates sum with s1+s2 on each update. Thats my code but it doesn't work. What I'm doing wrong?

Thanks in advance

DROP TRIGGER IF EXISTS `mTrigger`;
DELIMITER //
CREATE TRIGGER `mTrigger` BEFORE UPDATE ON `myTable`
FOR EACH ROW BEGIN

SELECT NEW.s1 + NEW.s2开发者_JS百科 INTO @sum;

SET @NEW.sum = @sum;

END
//
DELIMITER ;


try something like this:

delimiter #

create trigger myTable_before_update_trig before update on myTable
for each row
begin
  set new.sum = new.s1 + new.s2;
end#

delimiter ;
0

精彩评论

暂无评论...
验证码 换一张
取 消