views:

48

answers:

2

I have a table with rows like id, length, time and some of them are dublicates, where length and time is the same in some rows. I want to delete all copys of the first row submitted.

id | length | time
01 | 255232 | 1242
02 | 255232 | 1242 <- Delete that one

I have this to show all duplicates in table.

SELECT idgarmin_track, length  , time
FROM `80dage_garmin_track`
WHERE length in
       ( SELECT length
         FROM `80dage_garmin_track`
         GROUP
         BY length
         HAVING count(*) > 1 )
ORDER BY idgarmin_track, length, time LIMIT 0,500
+2  A: 
 DELETE FROM `80dage_garmin_track` t1
 WHERE EXISTS (SELECT 1 from `80dage_garmin_track` t2
          WHERE t1.Length = t2.Length
           AND t1.Time = t2.Time
           AND t1.idgarmin_track > t2.idgarmin_track)
Michael Pakhantsov
Thx for the solution! One problem, following error: "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't1WHERE EXISTS (SELECT 1 from `80dage_garmin_track` t2 WHERE t1.Le' at line 1" The query works fine with SELECT instead of DELETE.
Holsteinkaa
Another thing is, what is t1 and t2 ? variables?
Holsteinkaa
t1 and t2 are aliases. He omitted the keyword "AS". It`s not necessary. They are used, so you don`t have to write the whole table name again and again
tombom
+1 - much nicer than my answer :(
Martin
A: 

If you can take your table offline for a period, then the simplest way is to build a new table containing the data you want and then drop the original table:

create table `80dage_garmin_track_un` like `80dage_garmin_track`;

insert into `80dage_garmin_track_un`
select min(idgarmin_track), length, time 
group by length, time;

rename table `80dage_garmin_track` to old, `80dage_garmin_track_un` to `80dage_garmin_track`;

drop table old;
Martin