I’m having some problems with mySQL triggers.
I have this working:
CREATE TRIGGER `insert_trigger` AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM `table` WHERE userID = NEW.userID AND IP = new.IP) = 0 THEN
IF (SELECT COUNT(*) FROM `table` WHERE userID = NEW.userID) = 0 THEN
INSERT INTO `table2` VALUES(NEW.userID, (SELECT (COUNT(*)+1) FROM `table` WHERE userID = NEW.userID) );
ELSE
UPDATE `table2` SET views = views+1 WHERE table.userID = NEW.userID;
END IF;
END IF;
END//
DELIMITER ;
Which works fine with a INSERT statement.
However, I’m using REPLACE which seems to only call my DELETE trigger
CREATE TRIGGER `delete_trigger` BEFORE DELETE ON `table`
FOR EACH ROW
BEGIN
IF (SELECT views FROM `table2` WHERE userID = OLD.userID) > 1 THEN
UPDATE `table2` SET views = views-1 WHEREuserID = OLD.userID;
END IF;
END//
DELIMITER ;
When using REPLACE both DELETE and INSERT triggers should be called.
I’m aware that mySQL doesn’t support multiple triggers.
What is a possible solution to get the same results (adding/subtracting views) when another table has INSERT, DELETE and REPLACE events occurring.
The Problem lie in my own Trigger.
REPLACEcalls both Delete and Insert but this IF statementWas blocking the insert.