I need to DELETE
duplicated rows for specified sid on a MySQL
table.
How can I do this with an SQL query?
DELETE (DUPLICATED TITLES) FROM table WHERE SID = "1"
Something like this, but I don't know how to do it.
I need to DELETE
duplicated rows for specified sid on a MySQL
table.
How can I do this with an SQL query?
DELETE (DUPLICATED TITLES) FROM table WHERE SID = "1"
Something like this, but I don't know how to do it.
Love @eric's answer but it doesn't seem to work if you have a really big table (I'm getting
The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
when I try to run it). So I limited the join query to only consider the duplicate rows and I ended up with:The WHERE clause in this case allows MySQL to ignore any row that doesn't have a duplicate and will also ignore if this is the first instance of the duplicate so only subsequent duplicates will be ignored. Change
MIN(baz)
toMAX(baz)
to keep the last instance instead of the first.Following remove duplicates for all SID-s, not only single one.
With temp table
Since
temp_table
is freshly created it has no indexes. You'll need to recreate them after removing duplicates. You can check what indexes you have in the table withSHOW INDEXES IN table
Without temp table:
Could it work if you count them, and then add a limit to your delete query leaving just one?
For example, if you have two or more, write your query like this:
this removes duplicates in place, without making a new table
note: only works well if index fits in memory
I think this will work by basically copying the table and emptying it then putting only the distinct values back into it but please double check it before doing it on large amounts of data.
Creates a carbon copy of your table
Empties your original table
Copies all distinct values from the copied table back to your original table
Deletes your temp table.
You need to group by aLL fields that you want to keep distinct.
Suppose you have a table
employee
, with the following columns:In order to delete the rows with a duplicate
first_name
column: