remove repeated data in mysql

Asked

Viewed 418 times

-1

I have the following table called cadastro: Cod | name | age

With the data:

   COD   NOME     IDADE 
    2  | rafael  | 15
    3  | bruno   | 17
    4  | rafael  | 33
    5  | sabrina | 18
    6  | bruno   | 15
    7  | paulo   | 15
    8  | rafael  | 15

Note that some names repeat themselves, I need to run a command on my mysql to delete all repeated names and keep only 1, which contains the largest cod.

Example:

I have 3 records with the name raphael.

Then I’d just have to keep the 8 | rafael | 15.

This is possible to be done?

  • The table changes are part of the question’s scope to prevent this from happening?

1 answer

2


You can make a DELETE with INNER JOIN in the same table, however in INNER JOIN you will bring only the records where the cod is less than in the first table.

DELETE b FROM cadastro a
INNER JOIN cadastro b ON a.nome = b.nome AND b.cod < a.cod

Backup the table before running, I don’t have your table here to take the test.

  • That’s what it was, vlw.

  • I noticed one thing, when I run the command in a database with more than 100,000 records it takes a very, very long time. Is there any way to improve the performance?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.