MYSQL trigger to select from and update the same table gives error #1241 - Operand should contain 1 column(s)

1.7k views Asked by At

when i try to select and update the same table mysql gives error

error
#1241 - Operand should contain 1 column(s)

The trigger is

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$$
1

There are 1 answers

3
Bohemian On BEST ANSWER

The problem is you are selecting multiple columns (SELECT * FROM ...) into a single variable (@ifexists).

The solution is not to use a variable! Just use normal 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$$