gpt4 book ai didi

mysql - 当条件为真时,如何使用 MySQL 触发器更新多个表?

转载 作者:可可西里 更新时间:2023-11-01 08:20:57 26 4
gpt4 key购买 nike

我正在尝试创建一个触发器来更新另一个带有条件检查的表,但我一直收到 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 - 当条件为真时,如何使用 MySQL 触发器更新多个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10867611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com