2

Possible Duplicate:
Remove duplicate rows in MySQL

I have a table "recipientscore" that looks like this:

  • recipientid / messageid
  • 1 / 1
  • 2 / 2
  • 3 / 2
  • 4 / 2
  • 5 / 3
  • 6 / 4

What I want to do is delete all the records that appear twice or more. I do not want to keep one version of each duplicate records. It would look like this after clean up:

  • recipientid / messageid
  • 1 / 1
  • 5 / 3
  • 6 / 4

Would you have an idea on how to do that?

Thanks!

Community
  • 1
  • 1
user1029296
  • 609
  • 8
  • 17
  • I would take distinct records into a temp table, drop the old and rename the temp to old name ;-) – bonCodigo Dec 01 '12 at 17:56
  • I think this is _not_ a duplicate to the above question, since it's desired to drop **all** duplicates (i.e. _not_ drop all but one, and keep one). – Damon Dec 01 '12 at 22:27

2 Answers2

2
delete from recipientscore
where messageid in
(
   select * from
   (
      select messageid from recipientscore
      group by messageid
      having count(*) > 1
   ) x
)

SQLFiddle demo

juergen d
  • 201,996
  • 37
  • 293
  • 362
0

The answer given by 'juergen d' is perfect. You could try this if u wish to keep just one unique row and delete the remaining duplicate rows.

ALTER IGNORE TABLE recipient score  ADD UNIQUE KEY idx1(messageid); 
juergen d
  • 201,996
  • 37
  • 293
  • 362
Kanishk Dudeja
  • 1,201
  • 3
  • 17
  • 33