How to keep only one row of a table, removing duplicate rows?

前端 未结 8 1615
小蘑菇
小蘑菇 2020-12-08 16:41

I have a table that has a lot of duplicates in the Name column. I\'d like to only keep one row for each.

The following lists the duplicates, but I don\'t know how to

8条回答
  •  天命终不由人
    2020-12-08 16:55

    delete dup row keep one table has duplicate rows and may be some rows have no duplicate rows then it keep one rows if have duplicate or single in a table. table has two column id and name if we have to remove duplicate name from table and keep one. Its Work Fine at My end You have to Use this query.

    DELETE FROM tablename
    WHERE id NOT IN(
    
     SELECT id FROM
    (
        SELECT MIN(id)AS id
        FROM tablename
        GROUP BY name HAVING 
        COUNT(*) > 1
    )AS a )
    AND id NOT IN(
    (SELECT ids FROM
    (
    SELECT MIN(id)AS ids
        FROM tablename
        GROUP BY name HAVING 
        COUNT(*) =1
    )AS a1
    )
    )
    

    before delete table is below see the screenshot: enter image description here after delete table is below see the screenshot this query delete amit and akhil duplicate rows and keep one record (amit and akhil):

    enter image description here

提交回复
热议问题