【发布时间】:2014-02-01 16:07:54
【问题描述】:
在我将一些数据插入表之前的触发器中,我希望它检查我输入的事件日期的差异是否大于或等于 1 天。一个俱乐部每天只能举办一场活动。
示例故事
如果数据库中已经有2014-01-01 19:00:00 日期并且我正在尝试插入另一条日期为2014-01-01 的记录(小时无关紧要),它不应该允许。
触发器的部分代码
DECLARE k INT DEFAULT 0;
/* This is where I get the error, ABS is to make it always positive to go through
checking, so that it wont matter whether the NEW date is before or after */
SELECT ABS(DATEDIFF(DATE_FORMAT(`performance_date`, '\'%Y-%m-%d %H:%i:%s\''),
DATE_FORMAT(NEW.`performance_date`, '\'%Y-%m-%d %H:%i:%s\''))) INTO k;
/* Below code is out of scope for this question */
IF k = 0 THEN
SIGNAL SQLSTATE '58005'
SET MESSAGE_TEXT = 'Wrong! Only 1 performance in 1 club is allowed per day! Change your date, or club!';
END IF;
错误代码:1054。“字段列表”中的未知列“performance_date”
我尝试了一些简单的方法:
...DATEDIFF(`performance_date`, NEW.`performance_date`)
【问题讨论】:
标签: mysql date triggers date-format datediff