Wednesday 11 July 2012

Delete duplicate rows from Database

DELETE ta FROM tablename t1, tablename t2 WHERE t1.id > t2.id AND t1.fieldname = t2.fieldname
Above query will keep rows with lowest id of the table tablename.
DELETE ta FROM tablename t1, tablename t2 WHERE t1.id < t2.id AND t1.fieldname = t2.fieldname
And above query will keep rows with highest id of the table tablename.
Note: Above id is a field of integer type.

No comments:

Post a Comment