How to remove duplicate entries from a mysql db?

后端 未结 8 1352
南旧
南旧 2020-12-02 10:41

I have a table with some ids + titles. I want to make the title column unique, but it has over 600k records already, some of which are duplicates (sometimes several dozen ti

8条回答
  •  萌比男神i
    2020-12-02 11:05

    This shows how to do it in SQL2000. I'm not completely familiar with MySQL syntax but I'm sure there's something comparable

    create table #titles (iid int identity (1, 1), title varchar(200))
    
    -- Repeat this step many times to create duplicates
    insert into #titles(title) values ('bob')
    insert into #titles(title) values ('bob1')
    insert into #titles(title) values ('bob2')
    insert into #titles(title) values ('bob3')
    insert into #titles(title) values ('bob4')
    
    
    DELETE T  FROM 
    #titles T left join 
    (
      select title, min(iid) as minid from #titles group by title
    ) D on T.title = D.title and T.iid = D.minid
    WHERE D.minid is null
    
    Select * FROM #titles
    

提交回复
热议问题