1
Good evening folks. I am in need of a help to remove some duplicate table records from my Mysql 5.7 database
By running the following query I can identify which records are duplicated:
SELECT source_id, COUNT(*) TOTAL
FROM news
GROUP BY source_id
HAVING COUNT(*) > 1
I wonder how I can do to delete duplicate records and leave only the original after the SELECT command above.
And how do you identify which of the records with duplicate source_id is the original? If it is not allowed to duplicate it is not better to put this field as primary key or as UNIQUE?
– anonimo
Use the
SELECT DISTINCT
– Thiago Fernandes
If the table has a sequential ID it can simply Join the table with itself:
JOIN ON a.source_id = b.source_id AND id_sequencial != MIN( id_sequencial )
effectively filtering the "originals" with smaller id_unico. the returned id_unico will be the ones that should be deleted (test before, of course). If you don’t have ID id_unico, there’s a good chance you’ll have more serious problems than deletion. You can do everything in one operation, but test with a SELECT (subquery with Join) before switching to DELETE. Do not forget the most important, which is to fix the application so that the problem even happens.– Bacco