CREATE TEMPORARY TABLE tempficfac (PRIMARY KEY (id_cocabe)) SELECT id_cocabe,  soporte,  fecha FROM quartup1.cialdoco WHERE soporte='FV' + DELETE cialdocolin FROM quartup1.cialdocolin LEFT JOIN tempficfac ON cialdocolin.id_cocabe=tempficfac.id_cocabe WHERE cialdocolin.soporte='FV' AND tempficfac.fecha>='20150416' AND tempficfac.fecha<='20150423' + DELETE cialdoco FROM quartup1.cialdoco WHERE cialdoco.soporte='FV' AND cialdoco.fecha>='20150416' AND cialdoco.fecha<='20150423'