I have a few million database rows in MySQL InnoDB database which need to get cleaned up. Look at this example.
SELECT archiveid, clearingid, pickdate
FROM tblclearingarchive
WHERE clearingid = 30978729
ORDER BY pickdate;
+-----------+------------+---------------------+
| archiveid | clearingid | pickdate |
+-----------+------------+---------------------+
| 34328367 | 30978729 | NULL | *
| 34333844 | 30978729 | 2015-10-27 15:55:30 | <- keep only this row with oldest date
| 34438038 | 30978729 | 2016-03-01 10:34:25 | *
| 34481472 | 30978729 | 2016-04-20 13:44:19 | *
+-----------+------------+---------------------+
4 rows in set (0.01 sec)
So I know the clearingid value(s) of the affected field(s) and want to remove the one with no pickdate (null) and the two lines which are redundant (later, after first pick). In the example above, the ones marked with * should get deleted.
Any hints about how such SQL update/delete might look like?
There are about 30M rows and about 250K rows (known clearingid's) to clean up.
With the initial idea of Matias Barrios I found this solution to verify. It perfectly lists the rows I want to delete:
SELECT archiveid, clearingid, pickdate
FROM tblclearingarchive
WHERE clearingid = 30978729
AND (pickdate NOT IN (SELECT MIN(pickdate)
FROM tblclearingarchive
WHERE clearingid = 30978729 )
OR pickdate is NULL)
ORDER BY pickdate;
+-----------+------------+---------------------+
| archiveid | clearingid | pickdate |
+-----------+------------+---------------------+
| 34328367 | 30978729 | NULL |
| 34438038 | 30978729 | 2016-03-01 10:34:25 |
| 34481472 | 30978729 | 2016-04-20 13:44:19 |
+-----------+------------+---------------------+
3 rows in set (0.20 sec)
But I fail to delete using this sort of query:
DELETE FROM tblclearingarchive
WHERE clearingid = 30978729
AND (pickdate NOT IN (SELECT MIN(pickdate)
FROM tblclearingarchive
WHERE clearingid = 30978729 )
OR pickdate is NULL);
ERROR 1093 (HY000): You can't specify target table 'tblclearingarchive' for update in FROM clause