I’m working with wordpress and need to create a trigger that updates a table when another is updated. I created the trigger and she worked in theory, but it only updates the first record and pause its execution. Variable used to store a select that returns would be one of ids separated by commas.
Eq: The select returns something like: 424,532,123,212
And use it within an update by putting “in”.
UPDATE wp_posts SET e.post_modified = date (NEW.modified_date) e.ID WHERE IN (@ids);
And as I said it updates only the first record in this case would be 424
I wish someone would help me.
Here is the trigger:
CREATE TRIGGER triggerupdatedata AFTER UPDATE ON wp_ngg_gallery
FOR EACH ROW BEGIN
set @ids := (SELECT
GROUP_CONCAT(a.ID SEPARATOR ',')
FROM
wp_posts a, wp_postmeta b, wp_ngg_gallery c
WHERE
c.gid = OLD.gid
AND
a.ID = b.post_id
AND
b.meta_key = 'galeria_id'
AND
c.gid = (SELECT d.meta_value FROM wp_postmeta d WHERE d.post_id = a.ID AND d.meta_key = 'galeria_id')
);
UPDATE wp_posts e SET e.post_modified = date(NEW.modified_date) WHERE e.ID IN (@ids);
END;//
I think you have been bitten by the anti-pattern called implicit join syntax.
It causes all sorts of problems.
Make your joins explicit so you don’t end up with cross join spaggeti.
Also SQL and CSV do not mix.
But you can update a table and select from the same table in a sub-sub-select.
The reason is that a sub-sub-select is forced to run prior to the update, whereas a ‘mere’ subselect can run concurrent with the update, which would cause all sorts of problems.
Don’t save id values into a variable. Use the subquery directly in the UPDATE query.
Also, like Johan said – don’t use implicit join syntax.