Delete duplicate rows in a table

Asked

Viewed 51 times

2

I used the following SELECT to find the repeated values:

SELECT nome, Count(nome) FROM projeto
GROUP BY nome
HAVING Count(nome)>1

inserir a descrição da imagem aqui

Now I need to delete everything that is repeated and leave only the first that was inserted. I am trying to do it this way:

DELETE FROM projeto
WHERE nome IN
  (SELECT nome FROM projeto
   GROUP BY nome
   HAVING Count(nome)>1)
AND NOT id_projeto IN
  (SELECT Min(id_projeto) FROM projeto
   GROUP BY nome
   HAVING Count(nome)>1)

However, I am getting the following error: #1093 - You can’t specify target table 'project' for update in FROM clause

Any idea what might be wrong?

1 answer

1


There’s some way to do this, what comes to mind in a simple way is something like:

-- Obter apenas um ocorrencia de nome e guardar na temporaria.
select distinct nome into #tmp From projeto

-- Apagar dados originais.
Delete from projeto

-- Voltar a repor os dados na tabela original.
Insert into projeto select * from #tmp
  • #tmp would be a variable or a temporary table?

  • Temporary table.

  • I couldn’t solve it, Sergio. If I could detail a little more maybe it would be a little more clear to me.

  • Missing change * by Name, which is counted.

  • I was able to solve along with this question also: https://stackoverflow.com/questions/18288245/delete-duplicate-rows-in-mysql Thanks, Sergio!

Browser other questions tagged

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