问题
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