CREATE TEMPORARY TABLE records_tmp
SELECT MIN(`id`) AS id FROM `records` GROUP BY DATE_FORMAT('%Y-%m', `date`);
DELETE FROM `records` WHERE `id` NOT IN (SELECT id FROM records_tmp) AND DATE_FORMAT('%Y-%m', `date`) < DATE_FORMAT('%Y-%m', NOW());
DROP TEMPORARY TABLE records_tmp;