查找和删除重复数据


1、查找重复记录

1.查某一列(或多列)的重复值(只可以查出重复记录的值,不能查出整个记录的信息)

例如:查找stuid,stuname重复的记录

select stuid,stuname from stuinfo

group by stuid,stuname

having(count(*))>1

2.查某一列有重复值的记录(此方法查出的是所有重复的记录,如果有两条记录重复的,就查出两条)

例如:查找stuid重复的记录

select * from stuinfo

where stuid in (

select stuid from stuinfo

group by stuid

having(count(*))>1

)

3.查某一列有重复值的记录(只显示多余的记录,也就是说如果有三条记录重复的,就显示两条)

前提:需有一个不重复的列,此示例为recno。

例如:查找stuid重复的记录

select * from stuinfo s1

where recno not in (

select max(recno) from stuinfo s2

where s1.stuid=s2.stuid


2. 删除重复数据

[sql]  view plain copy
  1. delete a from tablea a where a.id not in (select min(b.id) from tablea b GROUP BY b.name)  
[sql]  view plain copy
  1. delete a from tablea a where a.id <> (select min(b.id) from tablea b where a.name=b.name)  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值