MySQL delete duplicate records but keep latest

后端 未结 7 1385

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:17

    If you want to keep the row with the lowest id value:

    DELETE n1 FROM 'yourTableName' n1, 'yourTableName' n2 WHERE n1.id > n2.id AND n1.email = n2.email
    

    If you want to keep the row with the highest id value:

    DELETE n1 FROM 'yourTableName' n1, 'yourTableName' n2 WHERE n1.id < n2.id AND n1.email = n2.email
    
    0 讨论(0)
提交回复
热议问题