Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql query to delete duplicate rows by keeping latest timestamp value?

I am facing a problem regarding the mysql table update. I have table that contains 5 columns including ID, NAME, TIMESTAMP and other 2 columns. My problem is I want to delete those rows whose both ID and NAME matches with another row,s ID and NAME with keeping the latest TIMESTAMP row.

I am trying the following query but it's showing the error:

#1093 - You can't specify target table 'dummy1' for update in FROM clause

The query is:

delete from dummy1  where LAST_USED_DATE not in(
select max(LAST_USED_DATE) from dummy1
group by ID,NAME);
like image 331
Kalpataru Mallick Avatar asked Oct 03 '22 19:10

Kalpataru Mallick


1 Answers

Try

DELETE d
  FROM dummy1 d JOIN 
(
  SELECT d1.id, d1.name, MAX(d1.last_used_date) max_date
    FROM dummy1 d1 JOIN dummy1 d2
      ON d1.id = d2.id AND d1.name = d2.name
   GROUP BY id, name
) q ON d.id = q.id AND d.name = q.name AND d.last_used_date <> q.max_date

Here is SQLFiddle demo

UPDATE To check only for duplicate ids you can tweak the above query a bit

DELETE d
  FROM dummy1 d JOIN 
(
  SELECT d1.id, MAX(d1.last_used_date) max_date
    FROM dummy1 d1 JOIN dummy1 d2
      ON d1.id = d2.id
   GROUP BY id
) q ON d.id = q.id AND d.last_used_date <> q.max_date

Here is SQLFiddle demo

UPDATE2 To delete dupes with max timestamp

1) you can by introducing a unique index with IGNORE option. In that case MySql will decide itself which records stay.

ALTER TABLE dummy1 ENGINE MyISAM;
ALTER IGNORE TABLE dummy1 ADD UNIQUE (id, name);
ALTER TABLE dummy1 ENGINE InnoDB;
like image 80
peterm Avatar answered Oct 07 '22 02:10

peterm