我每週參加一個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'表,因爲它已經被調用此存儲函數的語句使用/觸發。」在執行。 (你應該谷歌瞭解更多信息) –