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);
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With