UPDATE `vn`.`route` r JOIN(SELECT r.id, wl.workcenterFk FROM `vn`.`route` r JOIN `vn`.`routeLog` rl ON rl.originFk = r.id JOIN `vn`.`workerLabour` wl ON wl.workerFk = rl.userFk AND r.created BETWEEN wl.started AND IFNULL(wl.ended, r.created) WHERE r.created BETWEEN '2021-12-01' AND CURDATE() AND rl.action = 'insert' )sub ON sub.id = r.id SET r.commissionWorkCenterFk = sub.workcenterFk;