如何删除组中除最新行之外的所有行?

How to delete all rows in a group except the newest one?

说,我有一个 table 类似于:

CREATE TABLE `mytable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `application_id` int(11) NOT NULL,
  `company_name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

我想让 application_id 独一无二,但 table 中已经有一些重复项。如何按 application_id 分组并删除每组的所有记录,只留下 id 最高的记录?

delete from mytable
where id not in 
(
  select max(id)
  from mytable
  group by application_id
)