I am trying to create a trigger on a table (Cvr_flypn_hooks_501_1000) in my MySQL database that does the following:
Issue #1: I think my first issue is in the VALUES section, the last four value options I am trying to select the existing matching values to copy, but the trigger fails to fire when I add the additional existing data to the VALUES array so I think something is up here.
Issue #2: I realize that the query will run against the new data as well but I do not want it to compare its self to itself, I hope that makes sense.
I have included what I have so far below.
IF ( EXISTS(SELECT 1 FROM Cvr_flypn_hooks_501_1000 WHERE post_author_id = new.user_id)) then
INSERT INTO Cvr_flypn_1_link_501_1000 (a_hook_id, a_user_id, a_post_id, a_post_author_id, b_hook_id, b_user_id, b_post_id, b_post_author)
VALUES (new.hook_id,
new.user_id,
new.post_id,
new.post_author_id,
(SELECT hook_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id),
(SELECT user_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id),
(SELECT post_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id),
(SELECT post_author_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id));
END IF
Any and all help is greatly appreciated. I am pretty new to programming so please go gently.
Please try to remember the next time to provide data in a fiddle like below, so we don't have to recreate it al the time
A Sub-SELECT is only allowed to return 1 value so it is better to add an ORDER BY and A LIMIT 1.
CREATE tABLE Cvr_flypn_1_hooks_501_1000 (hook_id int, user_id int(11) ,post_id int ,post_author_id vaRCHAR(39))
INSERT INTO Cvr_flypn_1_hooks_501_1000 VALUES (1,1,1,"1")
CREATE TABLE account(hook_id int, user_id int(11) ,post_id int, post_author_id int)
CREATE TABLE Cvr_flypn_1_link_501_1000 (a_hook_id int, a_user_id int(11) ,a_post_id int, a_post_author_id int , b_hook_id int, b_user_id int, b_post_id int, b_post_author int)
CREATE TRIGGER upd_check AFTER INSERT ON account FOR EACH ROW BEGIN IF ( EXISTS(SELECT 1 FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id = new.user_id)) then INSERT INTO Cvr_flypn_1_link_501_1000 (a_hook_id, a_user_id, a_post_id, a_post_author_id, b_hook_id, b_user_id, b_post_id, b_post_author) VALUES (new.hook_id, new.user_id, new.post_id, new.post_author_id, (SELECT hook_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id ORDER BY hook_id DESC), (SELECT user_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id ORDER BY hook_id DESC), (SELECT post_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id ORDER BY hook_id DESC), (SELECT post_author_id FROM Cvr_flypn_1_hooks_501_1000 WHERE post_author_id=new.user_id ORDER BY hook_id DESC)); END IF; END
INSERT INTO account VALUES(1,1,1,1)
SELECT * FROM Cvr_flypn_1_link_501_1000
\na_hook_id | a_user_id | a_post_id | a_post_author_id | b_hook_id | b_user_id | b_post_id | b_post_author \n--------: | --------: | --------: | ---------------: | --------: | --------: | --------: | ------------: \n 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 \n
db<>fiddle here
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.