Search code examples
phpmysqlsqlcreate-table

Best way in MySQL to remove duplicated rows grouped by all except ID?


Doesn't seem to be a duplicate coz this question is different.

My table has 3 columns: id,col2,col3. I'm using this method to remove duplicated rows:

create table temp_table as
select * from my_table where 1 group by col2,col3;

drop table my_table;    
rename table temp_table to my_table;

However, my_table actually has a lot of columns, not just 3, painfully to list out in the query. So I want to ask whether there's a way that we can do a query something like this:

create table temp_table as
select * from my_table where 1 group by * except id;

drop table my_table;    
rename table temp_table to my_table;

Is there a possible way?


Solution

  • You could do a sub-query to make sure that what you get is unique. This query will give you the duplicates (preserving the ones with the lower IDs):

    SELECT id
      FROM duplicates d1
     WHERE EXISTS (
        SELECT 1
          FROM duplicates d2
         WHERE d2.col2 = d1.col2
           AND d2.col3 = d1.col3
           AND d2.id < d1.id
    )
    

    throw them into a temporary table (or load them to PHP) and run a second query to DELETE. (You can't modify a table while you're reading from it)

    Do a WHERE NOT EXISTS to get the IDs of the elements to preserve (again, the ones with the lowest IDs are kept).