我每周参加一个MySQL表..mysql的触发器不工作的更新查询
CREATE TABLE IF NOT EXISTS `students_attendance` (
`student_id` SMALLINT(5) UNSIGNED ZEROFILL NOT NULL,
`month` TINYINT UNSIGNED NOT NULL,
`w1` ENUM ('Absent', 'Present', 'Leave') NULL,
`w2` ENUM ('Absent', 'Present', 'Leave') NULL,
`w3` ENUM ('Absent', 'Present', 'Leave') NULL,
`w4` ENUM ('Absent', 'Present', 'Leave') NULL,
`w5` ENUM ('Absent', 'Present', 'Leave') NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`student_id`, `month`)
) ENGINE=InnoDB;
我使用触发器自动插入新行一个月
DELIMITER $$
CREATE TRIGGER update_students_attendance
BEFORE UPDATE ON `students_attendance` FOR EACH ROW
begin
DECLARE new_month TINYINT;
SET new_month = (SELECT month FROM students_attendance WHERE student_id = NEW.student_id AND month = NEW.month);
IF new_month is NULL OR new_month = ''
THEN
INSERT INTO students_attendance (student_id, month) VALUES (NEW.student_id, NEW.month);
END IF;
END;
$$
DELIMITER ;
每但是当我使用低于更新声明,它不插入任何行...
UPDATE students_attendance set `w1` = 'Absent' where `student_id` = '1' and `month` = '1'
任何人都可以帮助我?
您是否收到错误信息? –
@P.Salmon,不,它返回 - 0行受到影响。 (查询花费了0.0050秒) – santosh
即使您的逻辑正确,您也会得到一个“ERROR 1442(HY000):无法更新存储的函数/触发器中的'students_attendance'表,因为它已经被调用此存储函数的语句使用/触发。”在执行。 (你应该谷歌了解更多信息) –