Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to DELETE duplicate records in sql [duplicate]

Tags:

sql

php

mysql

Possible Duplicate:
How to delete duplicate rows with SQL?

I have a table with records and I want to delete all duplicate records

DELETE FROM 'table'
WHERE 'field' IN 
(
SELECT 'field' FROM 'table' GROUP BY 'field'
HAVING (COUNT('field')>1)
)

Why isn't this working?

like image 620
vdhmartijn Avatar asked Jan 31 '13 14:01

vdhmartijn


People also ask

Which command is used to remove duplicate record?

Remove duplicate lines with uniq The uniq command ensures that sequential identical lines are reduced to one.


2 Answers

Maybe you can explore with the command DISTINCT to select only unique records based in a field.

You can create a new table with the unique entries based. As an example...

CREATE TABLE nonDuplicates  
SELECT DISTINCT * FROM yourTable group by field
like image 74
peixe Avatar answered Oct 11 '22 17:10

peixe


This gives you more than one result:-

SELECT field FROM `table`
GROUP BY field
HAVING (COUNT('field)>1

Try to chenge this by:

SELECT TOP 1 field
FROM `table`
GROUP BY field 
HAVING (COUNT(field)>1
like image 44
Andrew Adamich Avatar answered Oct 11 '22 18:10

Andrew Adamich