MySQL Trigger not working very well

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

Read More

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;//

Related posts

Leave a Reply

2 comments

  1. 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.

    CREATE TRIGGER au_wp_ngg_gallery_each AFTER UPDATE ON wp_ngg_gallery FOR EACH ROW 
    BEGIN  
       UPDATE wp_posts e SET e.post_modified = date(NEW.modified_date) WHERE e.ID IN (
         SELECT * FROM (
       SELECT a.id  
       FROM wp_posts p
       INNER JOIN wp_postmeta pm ON  (pm.post_id  = p.id AND pm.meta_key  = 'galeria_id')
       INNER JOIN wp_postmeta pm2 ON (pm2.post_id = p.id AND pm2.meta_key = 'galeria_id')
       /* not sure if the join on pm2 is needed or not */
       INNER JOIN wp_ngg_gallery ng ON (ng.gid = pm2.meta_value)
       WHERE 
          ng.gid = OLD.gid ) sub) subsubhack); 
    END // 
    

    You cannot update a table and select from the same table in a subselect.

    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.