CREATE TEMPORARY TABLE tempficped (PRIMARY KEY (id_cocabe)) SELECT id_cocabe,  soporte,  fecha FROM quartup1.cialdoco WHERE soporte='PV' + DELETE cialdocolin FROM quartup1.cialdocolin LEFT JOIN tempficped ON cialdocolin.id_cocabe=tempficped.id_cocabe WHERE cialdocolin.soporte='PV' AND tempficped.fecha>='20160403' AND tempficped.fecha<='20160503' + DELETE cialdoco FROM quartup1.cialdoco WHERE cialdoco.soporte='PV' AND cialdoco.fecha>='20160403' AND cialdoco.fecha<='20160503'