How can I delete duplicate rows in a table

后端 未结 13 1395
情歌与酒
情歌与酒 2020-12-08 22:30

I have a table with say 3 columns. There\'s no primary key so there can be duplicate rows. I need to just keep one and delete the others. Any idea how to do this is Sql Serv

13条回答
  •  生来不讨喜
    2020-12-08 23:20

    I'm not sure if this works with DELETE statements, but this is a way to find duplicate rows:

     SELECT *
     FROM myTable t1, myTable t2
     WHERE t1.field = t2.field AND t1.id > t2.id
    

    I'm not sure if you can just change the "SELECT" to a "DELETE" (someone wanna let me know?), but even if you can't, you could just make it into a subquery.

提交回复
热议问题