gpt4 book ai didi

Mysql触发器不适用于更新查询

转载 作者:行者123 更新时间:2023-11-30 23:54:29 24 4
gpt4 key购买 nike

我有一个每周出勤的 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'

谁能帮帮我??

最佳答案

请检查下面提到的触发器。

DELIMITER $$
CREATE TRIGGER update_students_attendance
BEFORE UPDATE ON `students_attendance` FOR EACH ROW
begin
DECLARE new_month TINYINT DEFAULT 0;
SELECT month into new_month FROM students_attendance WHERE student_id = NEW.student_id AND month = NEW.month;
IF new_month is NULL OR new_month < 1 THEN
INSERT INTO students_attendance (student_id, month) VALUES (NEW.student_id, NEW.month);
END IF;
END;
$$
DELIMITER ;

关于Mysql触发器不适用于更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44493573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com