'Updating both parent and child table in stored procedure

MySQL Version: '5.7.37-0ubuntu0.18.04.1'

I have the goals of

  1. Updating rows in table ost_ticket after one week of no new activity
  2. Update or Insert into table ost_ticket__cdata one row for each row updated in ost_ticket

I will be using MySQL Event Scheduler to call the code that will accomplish the above two goals on an hourly basis.

My tendency is to put the code in a stored procedure, which is then called by the event scheduler.

My challenge is that I'm not sure how to transition from goal 1 to goal 2. How can I Update / insert into the child table for only those rows affected on the parent table, and only as a result of the event scheduler job (in other words, I don't want to use a trigger any time the parent table is updated, only when the "one-week-old" condition is satisfied)

Draft of the stored procedure with my rambling commentary is below...

USE `osticket`;
DROP procedure IF EXISTS `AutoClose';

DELIMITER //

Create Procedure AutoClose ()
Begin
    # GOAL 1: UPDATE ROWS THAT ARE ONE WEEK OLD... 
    UPDATE ost_ticket t 
    SET  
        t.status_id = 3,  # 3 = "Closed"   
        t.closed = date_add(t.lastupdate, interval 7 day),    
        t.updated = date_add(t.lastupdate, interval 7 day)  
        t.lastupdate = date_add(t.lastupdate, interval 7 day),     
    WHERE  t.status_id = 2  # 2 = "Resolved"
    AND now() >= date_add(t.lastupdate, interval 7 day);
    
    # GOAL 2: UPSERT ADDITIONAL INFORMATION ON CHILD TABLE
    # DONT THINK THIS WILL WORK HERE...  HOW DO I RUN THE FOLLOWING CODE 
    # FOR EACH ROW AFFECTED BY THE UPDATE ABOVE?
    
    INSERT INTO ost_ticket__cdata (ticket_id, subject, priority, autoclosed)
        ON DUPLICATE KEY UPDATE autoclosed = '1';
    values
    (x, y, 2, 1)

End //

DELIMITER;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source