I spent a lot of time on it, but I still can not resolve it.
For some reasons that I want prevent some same inserting to database. So I choose to use the trigger. That is,
CREATE TRIGGER trigger_card_usage_insert_block
BEFORE INSERT ON annual_card_usage_log
FOR EACH ROW
BEGIN
DECLARE lasttime INT; -- hold the last insert time
SELECT
usage_time INTO lasttime
FROM
annual_card_usage_log
WHERE
NEW.card_id = card_id
AND NEW.mch_id = mch_id
AND NEW.store_id = store_id
ORDER BY
usage_time DESC
LIMIT 1;
-- only insert when after 300 second
IF(
lasttime != NULL
AND(lasttime + 300 < new.usagetime)
) THEN
INSERT INTO annual_card_usage_log(
card_id ,
mch_id ,
store_id ,
usage_time
)
VALUES
(
NEW.card_id ,
NEW.mch_id ,
NEW.store_id ,
NEW.usage_time
);
END
IF;
END$
This is the creating code. And below is the table,
CREATE TABLE `annual_card_usage_log` (
`usage_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
`card_id` int(10) unsigned NOT NULL COMMENT '',
`mch_id` int(10) unsigned NOT NULL COMMENT '',
`store_id` int(10) DEFAULT NULL,
`usage_time` int(10) NOT NULL COMMENT '',
PRIMARY KEY (`usage_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Sadly I can not create the trigger, it seems wrong. Can you tell me why? Which code is wrong.
I use the mysql client called Sequel Pro, and when I run the sql I got a error message "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6 Execution stopped!" I don't now the reason.