remove duplicate rows based on one column value

后端 未结 4 547
清歌不尽
清歌不尽 2021-01-04 08:42

I have the below table and now I need to delete the rows which are having duplicate \"refIDs\" but have atleast one row with that ref, i.e i need to remove row 4 and 5. plea

4条回答
  •  花落未央
    2021-01-04 09:08

    In MySQL, you can do this with a join in delete:

    delete t
        from table t left join
             (select min(id) as id
              from table t
              group by refId
             ) tokeep
             on t.id = tokeep.id
        where tokeep.id is null;
    

    For each RefId, the subquery calculates the minimum of the id column (presumed to be unique over the whole table). It uses a left join for the match, so anything that doesn't match has a NULL value for tokeep.id. These are the ones that are deleted.

提交回复
热议问题