I am trying to create a trigger wherein it will insert a new row to a ADMIN_EMAIL_DOMAINS table.
CREATE TRIGGER email_domain_insert AFTER INSERT ON USERS
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT * FROM (SELECT DISTINCT SUBSTRING_INDEX(USR_EMAIL, '@', -1) as domain FROM USERS) as a WHERE a.domain = SUBSTRING_INDEX(NEW.USR_EMAIL, '@', -1)) THEN
INSERT INTO ADMIN_EMAIL_DOMAINS (DOMAIN_NAME, VISIBLE) VALUES (SUBSTRING_INDEX(NEW.USR_EMAIL, '@', -1), 1);
END IF;
END //
What I would like to do is everytime I create a new user, i would check its email extension and then insert it to ADMIN_EMAIL_DOMAINS table if that DOMAIN_NAME is not yet existing.
My problem is it has an SQL error when i tried to execute it
#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 '' at line 5
Am I missing something?
I could suggest you another way - make field DOMAIN_NAME
as unique and try to insert new domains into the table ADMIN_EMAIL_DOMAINS
with IGNORE option, eg -
DELIMITER $$
CREATE TRIGGER email_domain_insert
AFTER INSERT
ON users
FOR EACH ROW
BEGIN
INSERT IGNORE INTO ADMIN_EMAIL_DOMAINS (DOMAIN_NAME, VISIBLE) VALUES (SUBSTRING_INDEX(NEW.USR_EMAIL, '@', -1), 1);
END
DELIMITER ;
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.