I want to delete duplicate rows from my database table

☆樱花仙子☆ 提交于 2021-02-11 14:53:28

问题


I went through a few answers on this topic but none of them are executing on my workbench. I have duplicate rows in my table and I want to delete all retaining only one of them. First I grouped them by Count function and then tried deleting them but it isn't working. I tried running CTE query from old questions on the website:

 WITH ReferenceDP AS (
  SELECT[ReferenceId1], 
     row_number() OVER(PARTITION BY ReferenceId1 ORDER BY ReferenceId1) AS [rn]
  FROM TABLE
)
DELETE ReferenceDP WHERE [rn] > 1

But it did not work. I have attached the image of my table. Can someone help me out with the query. Thank You. DuplicateRowCount


回答1:


You can try to do this. It basically deletes duplicate rows and keeps the highest ReferenceId

 DELETE t1 FROM ast_fx_all_mst t1
 INNER JOIN ast_fx_all_mst  t2 
 WHERE 
    t1.ReferenceId < t2.ReferenceId AND 
    t1.TownAreaLocality = t2.TownAreaLocality AND
    t1.ContactName = t2.ContactName



回答2:


You can use the delete join query and reference your table twice.

DELETE t1 FROM ast_fx_all_mst t1
INNER JOIN ast_fx_all_mst t2 
WHERE 
    t1.ReferenceId < t2.ReferenceId AND t1.ContactName = t2.ContactName;

This link might help: https://www.mysqltutorial.org/mysql-delete-join/



来源:https://stackoverflow.com/questions/63735683/i-want-to-delete-duplicate-rows-from-my-database-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!