某某茶叶有限公司欢迎您!
金沙棋牌在线 > 必赢棋牌官网 > mysql删除重复记录的sql语句与查询重复记录(1/4)

mysql删除重复记录的sql语句与查询重复记录(1/4)

时间:2020-04-21 04:32

在A表中存在一个字段name,而且不同记录之间的name值有可能会相同,现在就是需要查询出在该表中的各记录之间,name值存在重复的项;Select Name,Count(*) from A Group By Name Having Count(*) 1如果还查性别也相同大则如下:Select Name,sex,Count(*) from A Group By Name,sex Having Count(*) 1(三)方法一declare @max integer,@id integerdeclare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) ; 1open cur_rowsfetch cur_rows into @id,@maxwhile @@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete from 表名 where 主字段 = @idfetch cur_rows into @id,@maxendclose cur_rowsset rowcount 0

方法1
delete yourtable
where [id] not in (
select max([id]) from yourtable
group by (name + value))
方法2
delete a
from 表 a left join(
select (id) from 表 group by name,value
)b on a.id=b.id
where b.id is null
查询及删除重复记录的sql语句
查询及删除重复记录的sql语句
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleid)来判断
select * from people
where peopleid in (select peopleid from people group by peopleid having count(peopleid) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleid)来判断,只留有rowid最小的记录
delete from people
where peopleid in (select peopleid from people group by peopleid having count(peopleid) > 1)
and rowid not in (select min(rowid) from people group by peopleid having count(peopleid )>1)
3、查找表中多余的重复记录(多个字段)
select *必赢棋牌官网, from vitae a
where (a.peopleid,a.seq) in (select peopleid,seq from vitae group by peopleid,seq having count(*) > 1)  1 2 3 4

方法二

delete yourtable where [id] not in ( select max([id]) from yourtable group by (name + value)) 方法2 delete a from 表 a left join( select (id) from 表 group by name,valu...

有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

1、对于第一种重复,比较容易解决,使用select distinct * from tableName

就可以得到无重复记录的结果集。

如果该表需要删除重复的记录,可以按以下方法删除select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp