sql 删除表中多余的重复记录(多个字段),只保留一条记录

时间:2021-04-14 20:14:23

在网上呢~自己收集了一些关于这方面的知识~  自己整理一下

1.查询重复记录

select * from 表名
where 重复字段 in (select 重复字段 from 表名 group by 重复字段 having count(重复字段) > 1)

2.删除保留一条重复记录只留有id最小的记录 


delete from 表名
where 重复字段 in (select 重复字段 from 表名 group by 重复字段 having count(重复字段) > 1)
and ID not in (select min(ID) from 表名 group by 重复字段 having count(重复字段 )>1)


3,查找表中多余的重复记录(多个字段),不包含id(伪列)最小的记录

select * from 表名
where 重复字段 in (select 重复字段 from 表名 group by 重复字段 having count(*) > 1)
and id not in (select min(id) from 表名 group by 重复字段 having count(*)>1)

4.消除一个字段的左边的第一位:

update 表名 set 消除字段 =left(消除字段,(len(消除字段-1)) where 消除字段 like '李%' 

5.消除一个字段的右边的第一位:

update 表名 set 消除字段 =Right(消除字段,(len(消除字段-1)) where 消除字段 like '%李'