Tab_1
"id" "name" "addr" "country" "status"
"1" "norman" "1st" "US" "2"
"2" "kirk" "2nd" "US" "2"
Tab_2
"id" "name" "addr" "country" "pos" "total"
"1" "norman" "1st" "US" "0" "0"
"2" "kirk" "2nd" "US" "0" "0"
当update
中的状态设置为Tab_1
时update
Tab_2
Tab_1
0
CREATE DEFINER=`root`@`localhost` TRIGGER `tab_2_upd` AFTER UPDATE ON `tab_1`
FOR EACH ROW BEGIN
if new.status = '0' then
update tab_2 set pos = pos+1, total = total+1 where id = new.id;
//Should it be new.id or old.id? I tried both but still no luck.
end if;
END
后{}}} 。问题是,即使它是正确的,它也什么都不做。我该如何设置呢?
{{1}}
答案 0 :(得分:1)
这是因为您没有更改分隔符而导致多个语句被破坏的另一种情况。这是一个非常常见的错误。
试试这个:
DELIMITER $$
DROP TRIGGER IF EXISTS `tab_2_upd`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `tab_2_upd` AFTER UPDATE ON `tab_1`
FOR EACH ROW BEGIN
if new.status = '0' then
update tab_2 set pos = pos+1, total = total+1 where id = new.id;
end if;
END$$
DELIMITER ;
ON UPDATE触发器同时具有OLD和NEW值,并且因为我认为id
的值不会改变(它不应该),所以使用任何值都是安全的(OLD或NEW) )。
对分隔符here有一个很好的答案。