Answer the question
In order to leave comments, you need to log in
How to update records in a BEFORE INSERT trigger?
There is a table:
ID
LOGIN_ID
Start
End
Session_ID
CREATE TRIGGER `CHECK_SESSION` BEFORE INSERT ON `data`
FOR EACH ROW BEGIN
SET @myVar1 = (SELECT `START` from `data` where `SESSION_ID` = NEW.SESSION_ID and `END` < new.END);
IF(@myVar1 IS NOT NULL) THEN
BEGIN
Insert into `data`(`LOGIN_ID`, `IP`, `SESSION_ID`, `SESSION_START`, `SESSION_TIMESTAMP`) values (new.LOGIN_ID, new.IP, new.SESSION_ID, new.SESSION_START, new.SESSION_TIMESTAMP) ON DUPLICATE KEY UPDATE `SESSION_TIMESTAMP` = new.SESSION_TIMESTAMP;
END;
END IF;
SET @myVar2 = (SELECT `START` from `data` where `SESSION_ID` = NEW.SESSION_ID and `END` > new.END);
IF(@myVar2 IS NOT NULL) THEN
BEGIN
declare msg varchar(128);
set msg = 'Filter!';
signal sqlstate '45000' set message_text = msg;
END;
END IF;
END
Answer the question
In order to leave comments, you need to log in
INSERT
INTO `table` (`LOGIN_ID`, `Start`, `End`, `Session_ID`)
VALUES (:loginId, :start, :end, :sessionId)
ON DUPLICATE KEY UPDATE `End` = GREATEST(`End`, :end)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question