CREATE TEMPORARY TABLE tempficopf (PRIMARY KEY (id_cocabe)) SELECT id_cocabe,  soporte,  fecha FROM quartup1.cialdoco WHERE soporte='RV' + DELETE cialdocolin FROM quartup1.cialdocolin LEFT JOIN tempficopf ON cialdocolin.id_cocabe=tempficopf.id_cocabe WHERE cialdocolin.soporte='RV' AND tempficopf.fecha>='20160101' AND tempficopf.fecha<='99991231' + DELETE cialdoco FROM quartup1.cialdoco WHERE cialdoco.soporte='RV' AND cialdoco.fecha>='20160101' AND cialdoco.fecha<='99991231'