我尝试在 phpmyadmin 上限制 MySQL 中的重叠时间段,因此新记录不能包含相同的 pc_id 和日期,因为您不能多次提供同一台计算机。请帮助我。
CREATE TRIGGER history_insert_overlap
BEFORE INSERT ON history
FOR EACH ROW
BEGIN
IF EXISTS (SELECT COUNT(*) FROM history WHERE `NEW.pc_id` =`pc_id` AND `NEW.date` BETWEEN `date` AND `date_return`) THEN
BEGIN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Interval Overlaps Existing Intervals';
END;
END IF;
END;
我收到错误 1064。
对于错误 1064 - sql 查询语法:将
NEW.pc_id
更改为 NEW。pc_id
看例子
CREATE TRIGGER history_insert_overlap
BEFORE INSERT ON history
FOR EACH ROW
BEGIN
IF EXISTS (SELECT 1 FROM history
WHERE NEW.`pc_id` =`pc_id`
AND NEW.`date` BETWEEN `date` AND `date_return`) THEN
BEGIN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Interval Overlaps Existing Intervals';
END;
END IF;
END;