i have MySQL data which is imported from csv file and have multiple duplicate files on it,
I picked all non duplicates using Distinct feature.
Now i need to delete all duplicates using SQL command.
Note i don’t need any duplicates i just need to fetch only noon duplicates
thanks.
for example if number 0123332546666 is repeated 11 time i want to delete 12 of them.
Mysql table format
ID, PhoneNumber
3
Answers
you could try using a left join with the subquery for min id related to each phonenumber ad delete where not match
otherwise if you want delete all the duplicates without mantain at least a single row you could use
Just
COUNT
the number of duplicates (withGROUP BY
) and filter byHAVING
. Then supply the query result toDELETE
statement:http://sqlfiddle.com/#!9/a012d21/1
complete fiddle:
schema:
delete query:
Instead of deleting from the table, I would suggest creating a new one:
Why? Deleting rows has a lot of overhead. If you are bringing the data in from an external source, then treat the first landing table as a staging table and the second as the final table.