我在phpMyAdmin中写了这个触发器
BEGIN
DECLARE start DATETIME;
DECLARE now DATETIME;
DECLARE date DATETIME;
SELECT `starting` INTO start FROM events WHERE events.id = NEW.event_id;
SET now = NOW();
SET date = CURDATE();
IF start > now THEN
IF DATE(start) = date THEN
SET @hours = HOUR(start - now);
IF @hours <= 1 THEN
SET @text = 'in less than an hour';
ELSE
SET @text = CONCAT('in about ', @hours, ' hours');
END IF;
INSERT INTO notifications (user_id, subject, action_id, data, seen, activates)
VALUES (NEW.user_id, NEW.event_id, 2, CONCAT('{"period":"', @text, '"}'), 0, now);
ELSE
INSERT INTO notifications (user_id, subject, action_id, data, seen, activates)
VALUES
(NEW.user_id, NEW.event_id, 2, '{"period":"tomorrow"}', 0, DATE(start - INTERVAL 1 DAY)),
(NEW.user_id, NEW.event_id, 2, '{"period":"in one hour"}', 0, start - INTERVAL 1 HOUR);
END IF;
END IF;
END
它的目的是在用户提交他将要参加活动时创建有关起始事件的通知。如果事件不是今天,则触发器有效,但如果事件是今天我收到此消息
列数据不能为NULL
列数据位于表notifications
中。如果事件设置为今天开始,我得到的列数据不能为NULL,如果事件将来会更进一步,明天我会收到准确的消息。
非常感谢任何帮助!