I have a MySQL Trigger I am trying to create, but can't get the syntax right.
The trigger should go through and match a set of keywords against the title of the new post inserted into the database. If it finds a match, it should assign the new post to that bucket and update the buckets set of keywords. If it does not find a match, it should create a new bucket using values from the post. The errors point to the line beginning with IF EXISTS, but I can't figure out what's wrong. Any help would be greatly appreciated. Thanks!
UPDATE: Code has been updated to pull out IF EXISTS, but now I am getting an error starting at the ELSE statement.
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;
$$