mysql - Find duplicates on multiple columns in a SQL table in order to create UNIQUE index -


i created index called abc on table called table primary key called id , 3 others int columns a, b , c can null.

now need index unique, tried :

alter table table drop index abc, add unique abc (a, b, c); 

but have duplicates, mysql answers :

#1062 - duplicate entry '1-2-3' key 'abc' 

i have lot of duplicates, i'm looking easy way search & destroy them all. first guess has been :

select * table group abc 

but sadly seems can't group indexes.

is there easy way find duplicates, keep 1 line of each duplicate , delete others ?

edit :

  • table id field primary key
  • a, b , c int , can null

no need eliminate duplicates first, use ignore option alter table want;

alter ignore table table drop index abc, add unique abc (a, b, c); 

an sqlfiddle test with.

if ignore not specified, copy aborted , rolled if duplicate-key errors occur. if ignore specified, 1 row used of rows duplicates on unique key. other conflicting rows deleted.

...and remember data before running potentially destructive sql random people on internet.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -