删除重复记录(Mysql,SqlServer,Sqlite)

时间:2022-11-04 05:13:25

Mysql中有重复的数据:

select resource_id,count() from t_resource_apptype_releation  GROUP BY resource_id having count()> order by count() desc

删除一下吧:

delete a from t_resource_apptype_releation as a,
(
select *,min(id) from t_resource_apptype_releation group by resource_id having count() >
) as b
where a.resource_id = b.resource_id and a.id > b.id;

创建一个唯一索引,防止再发生类似事件。

删除重复记录(Mysql,SqlServer,Sqlite)

现在开始玩SQL SERVER

查看:

select resource_id,count(1) from t_resource_apptype_releation  GROUP BY resource_id having count(1)>1 order by count(1) desc 

删除:

delete aa from t_resource_apptype_releation aa where  exists(select * from t_resource_apptype_releation where aa.id>id and  resource_id=aa.resource_id)

删除重复记录(Mysql,SqlServer,Sqlite)

Sqlite这样:

delete from T_RESOURCE_APPTYPE_RELEATION where id not in (select min(id) from T_RESOURCE_APPTYPE_RELEATION  group  by resource_id);