remove duplicate rows based on one column value

后端 未结 4 548
清歌不尽
清歌不尽 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 08:56

    This is similar to Gordon Linoff's query, but without the subquery:

    DELETE t1 FROM table t1
      JOIN table t2
      ON t2.refID = t1.refID
      AND t2.ID < t1.ID
    

    This uses an inner join to only delete rows where there is another row with the same refID but lower ID.

    The benefit of avoiding a subquery is being able to utilize an index for the search. This query should perform well with a multi-column index on refID + ID.

提交回复
热议问题