条件为真时,如何使用MySQL触发器更新多个表?
作者:互联网
我正在尝试创建一个触发器来更新带有条件检查的另一个表,但是我一直收到MySQL错误.我的触发器的代码是:
DELIMITER $$
CREATEb
TRIGGER visitor_validation
BEFORE INSERT ON ratingsvisitors
FOR EACH ROW
BEGIN
Set @counts=(SELECT count(*) from ratingsvisitors where visotrid=new.userid AND profileid=New.profileid AND itemid=New.itemid AND likevalidation=New.likevalidation AND likesordislike=New.likesordislike);
SET @ifexists = (SELECT * FROM ratingcounttracks WHERE userid=New.userid AND likedate=New.likevalidation AND countfor=New.likesordislike);
IF (@ifexists = NULL) THEN
INSERT INTO ratingsvisitors(userid, likedate, clickcount,countfor) values (New.userid, New.likevalidation ,'1',New.likesordislike);
ELSE
UPDATE ratingsvisitors SET clickcount=clickcount+1 WHERE userid=New.userid AND likedate=New.likevalidation AND countfor=New.likesordislike;
ENDIF;
END$$
MySQL显示错误:
#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 ‘; END’ at line 12
解决方法:
你可以做这样的事情
DELIMITER $$
CREATE
TRIGGER visitor_validation
BEFORE INSERT ON ratingsvisitors
FOR EACH ROW
BEGIN
Set @counts=(SELECT count(*) from ratingsvisitors where visotrid=new.userid AND
profileid=New.profileid AND itemid=New.itemid AND likevalidation=New.likevalidation AND
likesordislike=New.likesordislike);
IF NOT EXISTS (SELECT * FROM ratingcounttracks WHERE userid=New.userid AND
likedate=New.likevalidation AND countfor=New.likesordislike);
THEN
INSERT INTO ratingsvisitors(userid, likedate, clickcount,countfor) values (New.userid,
New.likevalidation ,'1',New.likesordislike);
ELSE
UPDATE ratingsvisitors SET clickcount=clickcount+1 WHERE userid=New.userid AND
likedate=New.likevalidation AND countfor=New.likesordislike;
ENDIF;
END$$
标签:mysql-error-1064,mysql,triggers 来源: https://codeday.me/bug/20191201/2079431.html