mysql delete row "almost" duplicated

2

The question is this:

|  id  |c1|c2| c3 |c4|fecha

817614  01 30 2517 0  2018-05-18 10:02:43

817615  02 30 2517 0  2018-05-18 10:02:46

817616  03 30 2517 0  2018-05-18 10:54:31

817617  04 30 2517 0  2018-05-18 10:54:39

817618  05 30 2554 0  2018-05-18 10:58:20

817619  06 30 2554 0  2018-05-18 10:58:28

817620  07 30 2554 0  2018-05-18 10:58:31

817621  08 30 2517 0  2018-05-18 10:58:54

817622  09 30 2444 0  2018-05-18 11:02:12

817623  10 30 2444 0  2018-05-18 11:02:17

817624  11 30 2444 0  2018-05-18 11:02:19

I have repeated records in all but the seconds of the Date column. And I can not use the distinct. How could I eliminate the records that are within the same minute? Thank you very much !!!

    
asked by roberto carrillo 18.05.2018 в 21:58
source

2 answers

1

for a SELECT

SELECT * FROM registros
  WHERE id 
  IN (
   SELECT t.id FROM (
     SELECT 
       DATE_FORMAT(r.fecha,'%Y-%m-%d %H:%i:00') AS fecha_sin_segundos,
       MIN(r.id) AS id 
     FROM registros r 
     GROUP BY fecha_sin_segundos
    ) AS t
);

for a DELETE (eye make backup) we put a NOT IN

DELETE FROM registros
  WHERE id 
  NOT IN (
   SELECT t.id FROM (
     SELECT 
       DATE_FORMAT(r.fecha,'%Y-%m-%d %H:%i:00') AS fecha_sin_segundos,
       MIN(r.id) AS id 
     FROM registros r 
     GROUP BY fecha_sin_segundos
    ) AS t
);

Take the record with id less, if you want to keep / show the id greater replace the MIN(r.id) by MAX(r.id)

to make a backup and test the cleaning you can use the following:

CREATE TABLE registros_backup LIKE registros;
INSERT registros_backup SELECT * FROM registros;

and you work on registros_backup

edited: For tables with many records you may want to create a new table and copy the data clean (add the "backup" with the select):

CREATE TABLE registros_limpio LIKE registros;
INSERT registros_limpio 
SELECT * FROM registros
  WHERE id 
  IN (
   SELECT t.id FROM (
     SELECT 
       DATE_FORMAT(r.fecha,'%Y-%m-%d %H:%i:00') AS fecha_sin_segundos,
       MIN(r.id) AS id 
     FROM registros r 
     GROUP BY fecha_sin_segundos
    ) AS t
);
    
answered by 18.05.2018 / 23:55
source
0

You can see the pattern that each record is repeated once, then eliminate the even ids. So you would only have one record left, always BCK (BACKUP) of your table.

delete from prueba WHERE mod(id,2) = 0

Best regards,

    
answered by 18.05.2018 в 23:03