USE vn; ALTER TABLE vn.duaTax DROP FOREIGN KEY duaTax_supplierFk; ALTER TABLE vn.duaTax DROP INDEX duaTax_fk2_idx; ALTER TABLE vn.duaTax DROP COLUMN supplierFk; UPDATE vn.duaTax d JOIN ( SELECT duaFk, taxClassFk , SUM(base) totalBase, rate, SUM(tax)totalTax FROM vn.duaTax GROUP BY duaFk, taxClassFk )sub ON sub.duaFk = d.id SET d.base = totalBase, d.tax = totalTax; DELETE d FROM vn.duaTax d LEFT JOIN ( SELECT id FROM vn.duaTax GROUP BY duaFk, taxClassFk )sub ON sub.id = d.id WHERE sub.id IS NULL;