0

Ok I know this might sound screwed up, but mysql sometime yesturday through an unexpected curball my way, which i have never come across before.

SO we have a database table called employees in that table you have records like

Russell
Smith
Sam

but all of a sudden i have

Russell
Russell
Smith
Smith
Smith
Sam
Sam
Sam

If anyone knows how to fix this without, downloading the full table and manually fixing it and re uploading that would be great

RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

2 Answers2

1

If your table has an id column and you want to keep only the records with lowest id you can do

delete from employees 
where id not in 
(
   select * from 
   (
     select min(id) 
     from employees 
     group by name
   ) x
)
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • . . I thought about this. But if the rows are being duplicated, wouldn't the id also be duplicated? If so, this isn't going to work. – Gordon Linoff May 28 '13 at 01:05
0

My recommendation:

(1) Create a new temporary table as:

create temporary table xxx as
    select distinct * from employees

(2) Truncate the original table

truncate table employees

(3) Insert the records back in

insert into employees
    select * from xxx

By truncating the table, you will be keeping almost all information associated with it -- security, triggers, constraints. The one issue may be an auto-incremented primary key. This may not work if you have one of those (but then again, you couldn't duplicate the records if there is a primary key).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786