read

This tip is a simple query to remove duplicated entries from a MySQL table.

DELETE FROM table1
USING  table1, 
       table1 as table2
WHERE  table2.id > table1.id
AND    table2.unique_param = table1.unique_param

Here I'm using a virtual copy of table1 to check whether it has uniqparam's after the first ocurrence of the _id, and then delete it.

It will only works if you have a sequencial 'id' column.
You can do a select before really deleting to check if it will do the proper thing.

Blog Logo

Endel Dreyer

Full-stack developer. Loves Ruby and JavaScript.


Published

Image

bugfixer / @endel

About programming, tools and solving problems.

Back to Overview