mysql触发更新无法正常工作

时间:2013-08-27 13:52:25

标签: mysql triggers

我希望在我的某个表格中插入时转换字段。

检查& ged's =>检查和geds。

DELIMITER $$
CREATE TRIGGER `perma_limk_insertion` AFTER INSERT
ON `tbl_magazine_subscription`
FOR EACH ROW BEGIN

    DECLARE magazine_name VARCHAR(100); 
    DECLARE magazine_name_from_table VARCHAR(100); 
    DECLARE magazine_name_new VARCHAR(100); 
    SET @magazine_name_from_table := (SELECT MAGAZINE_NAME FROM tbl_magazine WHERE MAGAZINE_ID = NEW.magazine_id LIMIT 1);
    SET @magazine_name            := REPLACE(@magazine_name_from_table,'&','and');
    SET @magazine_name_new        := REPLACE(@magazine_name,"'",'');
    UPDATE tbl_magazine SET perma_link = @magazine_name_new WHERE MAGAZINE_ID = NEW.magazine_id;
END$$
 DELIMITER ;

但使用上面的查询他们没有更新。如果我的查询中有任何错误

如果有人知道这件事,请帮助我。

1 个答案:

答案 0 :(得分:0)

尝试使用从任何地方删除“@”。并使用 INTO ,当您想要从表中选择一个值到变量时。

    DELIMITER $$ 
        CREATE TRIGGER perma_limk_insertion AFTER INSERT ON tbl_magazine_subscription FOR EACH ROW BEGIN

            DECLARE magazine_name VARCHAR(100); 
            DECLARE magazine_name_from_table VARCHAR(100); 
            DECLARE magazine_name_new VARCHAR(100); 

  //changed from SET to INTO here
  SELECT MAGAZINE_NAME INTO magazine_name_from_table FROM tbl_magazine
  WHERE MAGAZINE_ID = NEW.magazine_id LIMIT 1;

            SET magazine_name            := REPLACE(magazine_name_from_table,'&','and');
            SET magazine_name_new        := REPLACE(magazine_name,"'",'');
            UPDATE tbl_magazine SET perma_link = magazine_name_new WHERE MAGAZINE_ID = NEW.magazine_id;
        END$$

     DELIMITER ;

要了解更多aboue会话变量和局部变量,请点击此处

MySQL: @variable vs. variable. Whats the difference?