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!

2
  • I would take distinct records into a temp table, drop the old and rename the temp to old name ;-) Commented Dec 1, 2012 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). Commented Dec 1, 2012 at 22:27

2 Answers 2

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

SQLFiddle demo

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Juergen d, I tried your method, but I have the following error: #1093 - You can't specify target table 'recipientscore' for update in FROM clause. What would you advise me to do?
Try my update. You can trick MySQL to do this with a subselect.
When I do the select, it seems to me that it just works with rows that appear twice, not 3,4,5 times...
It works. See this SQLFiddle demo
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); 

1 Comment

But that will keep one of each group, doesn't it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.