Duplicate value in table. How to remove?

Asked

Viewed 880 times

5

inserir a descrição da imagem aqui

I inserted some TSE tables in my database and have duplicate column values SQ_CANDIDATO representing the image above. How can I do a maintenance with sql who will remove ALL the duplicated values independent of ID?

... Leave only one record SQ_CANDIDATO for each ID ...

  • it would not be better for her alone not?

  • 1

    See if you can help http://forum.imasters.com.br/topic/532628-deletar-registros-duplicates/ http://forum.imasters.com.br/topic/530320-values-duplicates/

  • Marcos removed the PHP tag from his question because it has no relation to his doubt the language itself.

  • See if this link helps you...
 
 http://stackoverflow.com/questions/4685173/delete-all-duplicate-rows-exceptit-for-one-in-mysql

2 answers

1

DELETE T2 /*Isto aqui define a tabela que terá o registro apagado*/
FROM candidatos T1
INNER JOIN candidados T2
ON T1.SQ_CANDIDATO = T2.SQ_CANDIDATO
AND T1.id < T2.id /*Isto aqui é para evitar que o JOIN faça select duas vezes no mesmo registro, isto é, uma vez em T1 e outra em T2, usando-se '>' ou '<', então o registro só aparecerá uma vez, então o desempenho do select será  maior e apenas um registro será apagado*/

1


You can mount a subselect inside the delete by selecting lines whose sq_candidato are repeated:

delete from candidatos where id in(
    select candidatos.id from candidatos
    inner join candidatos c on c.sq_candidato = candidatos.sq_candidato
    where c.id <> candidatos.id
    group by candidatos.sq_candidato
);

See working on Ideone. (Sqlfiddle is giving timeout)

Browser other questions tagged

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