create table email
( -- the columns here aren't great, but illustrative
id int auto_increment primary key,
email text not null,
rcvDate datetime not null
);
-- truncate table email;
insert email(email,rcvDate) values
('this is an email','2015-12-01 08:00:01'),
('greetings email','2015-12-01 09:00:01'),
('this is an email','2015-12-01 10:00:01'),
('this is an email','2015-12-01 11:00:01'),
('yet another email','2015-12-01 12:00:01');
select * from email;
+----+-------------------+---------------------+
| id | email | rcvDate |
+----+-------------------+---------------------+
| 1 | this is an email | 2015-12-01 08:00:01 |
| 2 | greetings email | 2015-12-01 09:00:01 |
| 3 | this is an email | 2015-12-01 10:00:01 |
| 4 | this is an email | 2015-12-01 11:00:01 |
| 5 | yet another email | 2015-12-01 12:00:01 |
+----+-------------------+---------------------+
The delete query
delete from email
where concat(email, id) not in
( select dummy
from
( select concat(email, max(id)) as dummy
from email
group by email
) xDerived
);
Results
select * from email;
+----+-------------------+---------------------+
| id | email | rcvDate |
+----+-------------------+---------------------+
| 2 | greetings email | 2015-12-01 09:00:01 |
| 4 | this is an email | 2015-12-01 11:00:01 |
| 5 | yet another email | 2015-12-01 12:00:01 |
+----+-------------------+---------------------+
Inspired from Martin Smith at https://stackoverflow.com/a/4606939/1816093
One could use rcvDate
for the max instead.