当我尝试选择和更新同一个表时,mysql 给出错误
error
#1241 - Operand should contain 1 column(s)
触发器是
DELIMITER $$
CREATE TRIGGER visitor_validation
BEFORE INSERT ON ratingsvisitors
FOR EACH ROW
BEGIN
SET @ifexists = (SELECT * FROM ratingcounttracks WHERE userid=New.vistorid AND likedate=New.likevalidation AND countfor=New.likeordislike);
IF (@ifexists = NULL) THEN
INSERT INTO ratingcounttracks(userid, likedate, clickcount,countfor) values (New.vistorid, New.likevalidation ,'1',New.likeordislike);
ELSE
UPDATE ratingcounttracks SET clickcount=clickcount+1 WHERE userid=New.vistorid AND likedate=New.likevalidation AND countfor=New.likeordislike;
END IF;
END$$
问题是您将多个列(SELECT * FROM ...
)选择到单个变量(@ifexists
)中。
解决方案是不使用变量!只需使用普通的SQL IF NOT EXISTS (...)
:
DELIMITER $$
CREATE TRIGGER visitor_validation
BEFORE INSERT ON ratingsvisitors
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT * FROM ratingcounttracks WHERE userid=New.vistorid AND likedate=New.likevalidation AND countfor=New.likeordislike) THEN
INSERT INTO ratingcounttracks(userid, likedate, clickcount,countfor) values (New.vistorid, New.likevalidation ,'1',New.likeordislike);
ELSE
UPDATE ratingcounttracks SET clickcount=clickcount+1 WHERE userid=New.vistorid AND likedate=New.likevalidation AND countfor=New.likeordislike;
END IF;
END$$