MySQL delete duplicate records but keep latest

后端 未结 7 1415

I have unique id and email fields. Emails get duplicated. I only want to keep one Email address of all the duplicates but with the latest id<

7条回答
  •  醉梦人生
    2020-11-28 23:11

    Correct way is

    DELETE FROM `tablename` 
      WHERE id NOT IN (
        SELECT * FROM (
          SELECT MAX(id) FROM tablename 
            GROUP BY name
        ) 
      )
    

提交回复
热议问题