我想让这段代码工作,但没有用。。

DELIMITER $$

CREATE
    TRIGGER `update_tbl1` AFTER UPDATE
    ON `tbl1`
    FOR EACH ROW BEGIN

        IF (SELECT count(*) FROM tbl1 WHERE stn=NEW.stn) = 1
        THEN
        UPDATE tbl2 SET date_posted=NEW.date_posted WHERE stn=NEW.stn;
        ELSE
        INSERT INTO tbl2 (stn) VALUES (NEW.stn);
        END IF

    END$$

DELIMITER ;

我有两个表,我想要一个触发器,如果TBL1被更新,只要TBL2上已经存在数据,否则,将插入TBL2。我的代码似乎可行,错误似乎与语法有关,但我找不到位置。
编辑:
错误如下:
 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO tbl2 (stn) VALUES (NEW.stn); END IF END' at line 10

最佳答案

关于语法错误,我认为这正是您在;之后忘记分号的原因,请尝试以下操作:

DELIMITER $$

CREATE
    TRIGGER `update_tbl1` AFTER UPDATE
    ON `tbl1`
    FOR EACH ROW BEGIN

        IF (SELECT count(*) FROM tbl1 WHERE stn=NEW.stn) = 1
        THEN
        UPDATE tbl2 SET date_posted=NEW.date_posted WHERE stn=NEW.stn;
        ELSE
        INSERT INTO tbl2 (stn) VALUES (NEW.stn);
        END IF;

    END$$

DELIMITER ;

07-28 10:17