说,我有一张与此类似的表:
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
唯一,但是表中已经有一些重复项。如何按application_id
分组并删除每组中的所有记录,仅留下具有最高id
的记录?
delete from mytable
where id not in
(
select max(id)
from mytable
group by application_id
)