mysqlmariadbmysql-error-1064

Creating a trigger to add new row to another table after new record using if statement


I cannot seem to get this trigger to work. I keep getting this error.

#1064 - You have an error in your SQL syntax; check the manual 
that corresponds to your MariaDB server version for the right syntax to 
use near '' at line 6
SELECT * FROM website_queries;
CREATE DEFINER=`name`
    TRIGGER `add_subscribed_users` 
    AFTER INSERT ON `website_queries` FOR EACH ROW
BEGIN
IF NEW.subscribed = 0 THEN
    INSERT INTO users SET id=DEFAULT, firstName = NEW.firstName, lastName = NEW.lastName, mobile=DEFAULT, email = NEW.email, admin=DEFAULT, createdAt = NEW.createdAt, subscribed = NEW.subscribed;
END IF;
END; //

I am using 10.5.15-MariaDB-cll-lve doing this through phpMyAdmin


Solution

  • You do not need in IF.

    CREATE DEFINER=`name` TRIGGER `add_subscribed_users` 
    AFTER INSERT ON `website_queries` 
    FOR EACH ROW
        INSERT INTO users (firstName, lastName, email, createdAt, subscribed)
        SELECT NEW.firstName, NEW.lastName, NEW.email, NEW.createdAt, NEW.subscribed
        WHERE NEW.subscribed = 0;
    

    In this single-statement form you do not need in BEGIN-END and DELIMITER too.