简体   繁体   中英

MySQL trigger syntax error (based on two tables, with if statement)

I'm getting a syntax error while trying to create a trigger. I'm far from MySQL expert but managed to create the following code based on info I found on stackoverflow. The purpose of the trigger is to check if the sales row relates to an auction item (rather than a buy now item) and if it does then calculate a premium of 10% on the sale price (insert the premium amount into the premium column). "listing_id" is the link between the listings table and sales listings table.

DELIMITER $$
CREATE TRIGGER add_premium
AFTER INSERT
   ON sales_listings FOR EACH ROW
BEGIN
   SET @listing_type := (SELECT listing_type
                     FROM listings
                     WHERE listings.listing_id = NEW.listing_id);
   IF @listing_type = 'auction' THEN
            NEW.premium = NEW.price * 0.1;
   ELSE
        NEW.premium = 0;
   END IF;
END$$
DELIMITER;

This is the syntax error:

 #1064 - 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 '.premium = NEW.price * 0.1;
   ELSE
            NEW.premium = 0;
   END IF;
' at line 9 

What am I doing wrong?

An assignment needs to be preceded by SET .

IF @listing_type = 'auction' THEN
    SET NEW.premium = NEW.price * 0.1;
ELSE
    SET NEW.premium = 0;
END IF;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM