select*from 表名 where
重复字段名 in(select 重复字段名 from 表名 groupby 重复字段名 havingcount(1)>1)orderby 重复字段名
查询出所有重复记录并且删除多余数据,只保留id最小的一条
deletefrom 表名
where
重复字段名 in(SELECT a.重复字段名 from(select 重复字段名
from 表名
groupby 重复字段名 havingcount(1)>1) a
)and
id notin(SELECT b.id from(selectmin(id)as id
from 表名
groupby 重复字段名 havingcount(1)>1) b
)
实战:删除x表的i列的重复数据,保留重复数据中ID最小的一条数据
表:x
重复数据列:i
重复数据保留最小ID:id
脏数据
id
i
1
a
2
a
3
b
4
b
清理后数据
id
i
1
a
3
b
deletefrom x
where
i in(SELECT a.i from(select i
from x
groupby i havingcount(1)>1) a
)and
id notin(SELECT b.id from(selectmin(id)as id
from x
groupby i havingcount(1)>1) b
)
deletefrom 表名
where(重复字段名1, 重复字段名2)in(select a.重复字段名1,a.重复字段名2from(select 重复字段名1,重复字段名2from 表名
groupby 重复字段名1,重复字段名2havingcount(1)>1) a
)and
id notin(select b.id from(selectmax(id) id
from 表名
groupby 重复字段名1,重复字段名2havingcount(1)>1) b
)
实战:删除x表的i列+j列的重复数据,保留重复数据中ID最大的一条数据
表:x
重复数据列:i,j
重复数据保留最大ID:id
脏数据
id
i
j
1
a
x
2
a
x
3
b
y
4
b
y
清理后数据
id
i
j
2
a
x
4
b
y
deletefrom x
where(i, j)in(select a.i,a.j from(select i,j
from x
groupby i,j havingcount(1)>1) a
)and
id notin(select b.id from(selectmax(id) id
from x
groupby i,j havingcount(1)>1) b
)
查询出所有重复记录select 字段名,字段名from 表名where 重复字段名 in (select 重复字段名 from 表名 group by 重复字段名 having count(1) >= 2) ORDER BY 重复字段名查询出所有重复记录并且删除多余的只保留一条delete from 表名where 重复字段名 in ( SELECT a.重复字段名from( select 重复字段名 from 表名 group by 重复字段名