我試圖讓這段代碼工作,但無濟於事..MySQL的觸發器的if-else INSERT,UPDATE
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 ;
我有兩個表,我想一個觸發器,將更新TBL2如果TBL1是隻有當數據已經存在於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
我很尷尬,但感謝你的鷹眼前! :) – xjshiya