Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySql After Insert Trigger using a Select clause

The following query generates a random string of 20 characters:

SELECT LEFT(MD5(RAND()), 20); 

My aim is to insert this into the token field of the users_address field when a new row is added. I created the following trigger:

BEGIN       
    INSERT INTO users_address (token) VALUES (SELECT LEFT(MD5(RAND()), 20));
END

which returns the error "You have an error in your SQL syntax; check the manual…". If I replace the SELECT statement with an actual string, the trigger is created so the issue seems to be the SELECT. What do I need to do to change to make it work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

SELECT is not needed in VALUES Clause :

INSERT INTO users_address (token) VALUES (LEFT(MD5(RAND()), 20));

Demo here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading