我有一个我正在尝试创建的 MySQL 触发器,但语法不正确。
触发器应该通过并匹配一组关键字与插入数据库的新帖子的标题。如果找到匹配项,则应将新帖子分配给该存储桶并更新存储桶的关键字集。如果未找到匹配项,则应使用帖子中的值创建一个新存储桶。错误指向以 IF EXISTS 开头的行,但我不知道出了什么问题。任何帮助将不胜感激。谢谢!
更新:代码已更新以退出 IF EXISTS,但现在我从 ELSE 语句开始出现错误。
delimiter $$
CREATE TRIGGER bucket_trigger_v1 BEFORE INSERT ON posts
FOR EACH ROW
BEGIN
set @target = NEW.post_title;
set @bucket_id_number = '';
set @bucket_relevance = '';
SELECT idbuckets, MATCH (keywords) AGAINST (@target) AS score INTO @bucket_id_number, @bucket_relevance FROM buckets WHERE MATCH (keywords) AGAINST (@target) ORDER BY score DESC LIMIT 1;
IF (@bucket_id_number != '') THEN
BEGIN
SET NEW.buckets_idbuckets = @bucket_id_number;
UPDATE buckets SET keywords = concat(keywords, @target) WHERE idbuckets = @bucket_id_number;
END
ELSE
BEGIN
INSERT INTO buckets (idbuckets, keywords, creationdate) VALUES (, @target, NEW.postdate);
SET NEW.bucket_idbuckets = (SELECT LAST_INSERT_ID());
END
END IF;
END;
$$