0

Here is my SQL, but the MySQL server consistently says that it has an error in the SQL syntax. I have tried many times, but it doesn't work. Please Help!

DELIMITER //
CREATE TRIGGER Score_Count_for_Contestant
AFTER INSERT ON award
FOR EACH ROW
BEGIN
 IF NEW.award = 'gold'AND contestant.CID=new.CID THEN
  UPDATE contestant SET contestant.score= contestant.score+10

IF new.Award='silver' AND contestant.CID=new.CID THEN
   UPDATE contestant SET contestant.score= contestant.score+8

IF new.Award='bronze' AND contestant.CID=new.CID THEN
   UPDATE contestant SET contestant.score= contestant.score+6
 END IF
END //

DELIMITER ;
2
  • Missing ; delimiters after the end of your UPDATE queries. Commented Dec 20, 2015 at 15:39
  • Can you post the award and contestant structures? Commented Dec 20, 2015 at 15:39

1 Answer 1

1

I would eliminate the if and simplify the logic:

UPDATE contestant c
    SET c.score = (case when NEW.award = 'gold' then c.score + 10
                        when NEW.award = 'silver' then c.score + 8
                        when NEW.award = 'bronze' then c.score + 6
                        else c.score
                   end)
    WHERE c.CID = new.CID

You can add this condition to the where clause NEW.award IN ('gold', 'silver', 'bronze') if there are lots of other awards.

Your code doesn't work because you are referencing contestant.CID in the IF, and it is not defined. Note: you could still have an issue with the delimiter, but that is unclear from the code sample.

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't notice the contestant reference, removing my original answer then (as fixing the delimiters would not solve the problem). Thanks for pointing it out.
Thank you very much Gordon Linoff! I love you!!! It works! And you make up my mind

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.