Kalpataru Mallick
Kalpataru Mallick

Reputation: 83

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);

Upvotes: 5

Views: 4261

Answers (2)

peterm
peterm

Reputation: 92785

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;

Upvotes: 3

Mark
Mark

Reputation: 8431

Even without fiddle I give it a shot. Try this:

DELETE D1.* FROM DUMMY1 D1
JOIN (SELECT D.ID,D.NAME, MAX(LAST_USED_DATE) DATE FROM DUMMY1 D GROUP BY D.ID) D2 ON D1.ID = D2.D2
WHERE D1.NAME = D2.NAME AND D1.LAST_USED_DATE <> D2.DATE

I get the reverse since you are keeping the MAX(LAST_USED_DATE)

Upvotes: 1

Related Questions