DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`workerTimeControl_remove`(IN vUserFk INT, IN vTimed DATETIME) BEGIN DECLARE vDirectionRemove VARCHAR(6); DECLARE vDirectionPrevious VARCHAR(6); DECLARE vTimedPrevious DATETIME; SELECT direction INTO vDirectionRemove FROM vn.workerTimeControl WHERE userFk = vUserFk AND timed = vTimed ; IF vDirectionRemove = 'out' THEN SELECT timed, direction INTO vTimedPrevious, vDirectionPrevious FROM vn.workerTimeControl WHERE userFk = vUserFk AND timed < vTimed ORDER BY timed DESC LIMIT 1; IF vDirectionPrevious = "middle" THEN UPDATE vn.workerTimeControl SET direction = "out" WHERE userFk = vUserFk AND timed = vTimedPrevious; END IF; ELSE IF vDirectionRemove = 'in' THEN UPDATE vn.workerTimeControl SET direction = "in" WHERE userFk = vUserFk AND timed > vTimed ORDER BY timed ASC LIMIT 1; END IF; END IF; DELETE FROM vn.workerTimeControl WHERE userFk = vUserFk AND timed = vTimed; CALL vn.workerTimeControlSOWP(vUserFk, vTimed); END$$ DELIMITER ;