use vn; DROP TEMPORARY TABLE IF EXISTS tWorkerLabour ; CREATE TEMPORARY TABLE tWorkerLabour SELECT l.workerFk, l.businessFk FROM vn.workerLabour l WHERE util.CURDATE() BETWEEN l.started AND IFNULL(l.ended, util.CURDATE()); UPDATE vn.worker w JOIN tWorkerLabour l ON l.workerFk = w.id SET w.businessFk = l.businessFk; DROP TEMPORARY TABLE tWorkerLabour;