Delete all selected rows from the database except the first two

I need to delete all dates from the database except the first two selected dates:

SELECT DateColumn 
FROM Table 
GROUP BY date(DateColumn) 
ORDER BY DateColumn DESC

I want all dates except the first two to be deleted in one request.

+3
source share
2 answers
delete from table where DateColumn not in(
SELECT DateColumn 
FROM Table 
GROUP BY date(DateColumn) 
ORDER BY DateColumn DESC LIMIT 2)
+4
source
DELETE FROM Table
      WHERE DateColumn NOT IN (SELECT DateColumn 
                                 FROM Table 
                             GROUP BY date(DateColumn) 
                             ORDER BY DateColumn DESC
                                LIMIT 2);
+3
source

Source: https://habr.com/ru/post/1536406/


All Articles