MySQL删除重复记录只保留一条

时间:2022-09-22 22:59:40

需要分步处理,思路是先建立一张临时表,然后把要删除的记录id存入临时表中,然后对比临时表内的数据,删除主表里的数据。

sql如下:

1、创立临时表,并插入需要删除的表数据的id,只保留每批重复数据中id值最大的那条记录。

create table tmp select c.id from
(
SELECT id FROM
(
SELECT a.* FROM `crawl_log` a,
(SELECT question FROM `crawl_log` GROUP BY question HAVING count(question) >1) b
where a.question = b.question
)reData
where reData.id not in
(
select max(id) from `crawl_log` group by question having count(question)>1
)
)c;


2、联合临时表,删除重复数据

DELETE from `crawl_log` where id in(SELECT id from tmp);

3、删除临时表