Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate rows in a table having no primary key

I have a table item that contains items like

name
------
alpha
alpha 
beta
charlie
charlie

In this case how would I delete duplicate rows but one record should remain. The above table does not have any primary key.

like image 359
sureyn Avatar asked Oct 06 '22 08:10

sureyn


1 Answers

Try this

DELETE FROM item WHERE GREATEST(0,@num := IF(NAME = @NAME, @num + 1, 0),LEAST(0, LENGTH(@NAME := NAME)))>0
like image 64
neeraj Avatar answered Oct 11 '22 17:10

neeraj