1

I'm trying to delete duplicate rows from my database, that has the same 'input' column, and 'response' column. Right now, I have a query that SELECT all the duplicates, but I'm not sure how to write the query to delete the duplicates:

SELECT * , COUNT( * ) AS matches 
FROM allData 
GROUP BY input, response 
HAVING matches > 1 

If I'm writing the DELETE query, I think it would something like

DELETE FROM allData WHERE blah = blah

^---But that doesn't let me select the 'count(*)' or 'group by', so I'm not really sure how to properly write this.

halfer
  • 19,824
  • 17
  • 99
  • 186
frosty
  • 2,559
  • 8
  • 37
  • 73

1 Answers1

1

Try this;)

DELETE 
    T1
FROM allData T1
INNER JOIN (
    SELECT input, response
    FROM allData 
    GROUP BY input, response 
    HAVING COUNT(1) > 1 
) T2 ON T1.input = T2.input AND T1.response = T2.response

Edited:

DELETE T1
FROM allData T1
INNER JOIN allData T2 ON T1.input = T2.input AND T1.response = T2.response AND T1.id > T2.id

This will remain the record which has the minimum id. And take a look of Delete all duplicate rows except for one in mysql.

Community
  • 1
  • 1
Blank
  • 12,308
  • 1
  • 14
  • 32