CREATE TEMPORARY TABLE tempficalb (PRIMARY KEY (id_cocabe)) SELECT id_cocabe,  soporte,  fecha FROM quartup1.cialdoco WHERE soporte='AV' + DELETE cialdocolin FROM quartup1.cialdocolin LEFT JOIN tempficalb ON cialdocolin.id_cocabe=tempficalb.id_cocabe WHERE cialdocolin.soporte='AV' AND tempficalb.fecha>='20151210' AND tempficalb.fecha<='20160109' + DELETE cialdoco FROM quartup1.cialdoco WHERE cialdoco.soporte='AV' AND cialdoco.fecha>='20151210' AND cialdoco.fecha<='20160109'