如何用sql语句查询和删除表中重复数据

1、查询表中重复数据(单字段)
Select * From
表 Where 字段1 In (Select 字段1 From 表 Group By 字段1 Having Count(字段1) > 1)

 Delete From exam_testdb t
Where t.name In (Select s.name From exam_testdb s Group By 
s.name  Having Count(s.name) > 1) And
  
Rowid Not In (Select Min(Rowid) From exam_testdb m Group By m.name Having Count(m.name) > 1)

 
Select * From exam_testdb t Where t.name In (Select s.name From exam_testdb s Group By s.name Having Count(t.name) > 1)

2、删除表中多余的重复记录,只留有rowid最小的记录(单字段)
Delete From
Where 字段1 In (Select 字段1 From 表 Group By 字段1 Having Count(字段1) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1 Having Count(字段
1) > 1)
     

3、查找表中多余的重复记录(多个字段)
Select * From
表 a Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段2 Having Count(*) > 1)

4、删除表中多余的重复记录,只留有rowid最小的记录(多个字段)
Delete From a
Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段
2 Having Count(*) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1, 字段
2 Having Count(*) > 1)
     
5.删除多于的重复记录(单个字段,多个字段)

delete from table where id not in ( select min(id) from table group by name)
或者
delete from table where id not in ( select min(id) from table group by 字段1,字段2)

6.删除多余的重复记录(单个字段,多个字段)
delete from table where id in ( select max(id) from table group by name having count(*)>1)
 

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
Select *
From
a
Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段
2 Having Count(*) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1, 字段2 Having Count(*) > 1)


查单个字段的重复次数
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值