2

I've got table with this data

id, archive id, ean, index, date, (...)

I've got some items with same archive id, same ean, but different index.

So in this case, I want to delete older (basing on date) item, so result will be that for each combination archive_id/index there will be no more than 1 result.

3 Answers 3

2

The following (untested) should work:

DELETE FROM someTable WHERE EXISTS ( SELECT id FROM someTable AS subqTable WHERE
subqTable.id = someTable.id
AND subqTable.ean = someTable.ean
-- and other equality comparisons
AND subqTable.date AFTER someTable.date)
Sign up to request clarification or add additional context in comments.

4 Comments

is there a way to test it on some limited (like 10 items) count of items before running it for entire table?
Yes, you can use a LIMIT in a DELETE statement: dev.mysql.com/doc/refman/5.5/en/delete.html
btw Im not sure for what you use .id - this completly does not matter in my case. Is it needed? Need to keep only 'the freshest' one of results of some combination of (archive_id + index)
No, it's not, I am just not sure if MySQL optimizes such selects also better without the wildcard *.
0
DELETE duplicates.*
FROM _table
JOIN _table AS duplicates
    ON (_table.archive_id = duplicates.archive_id AND _table.index = duplicates.index)
WHERE duplicates.date < _table.date;

Comments

0
delete t1
from your_table t1
left join 
(
  select archive_id, ean, min(date) as mdate
  from your_table
  group by archive_id, ean
) t2 on t1.archive_id = t2.archive_id
     and t1.ean = t2.ean
     and t1.date = t2.mdate
where t2.mdate is null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.