一、查找重复记录
1.查找全部重复记录
单字段:
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
多字段:
select id from 表 a where id !=(select max(id) from 表 b where a.[OrderNo]=b.[OrderNo] and a.[OrderType]=b.[OrderType] and a.[LNID]=b.[LNID])
2.过滤重复记录(只显示一条)
Select * From HZT Where ID In (Select Max(ID) From HZT Group By Title)
注:此处显示ID最大一条记录
二、删除重复记录
1.删除全部重复记录(慎用)
单字段:
Delete 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
多字段:
Delete FROM dbo.table where id in ( select id from dbo.table a where id !=(select max(id) from dbo.table b
where a.[OrderNo]=b.[OrderNo] and a.[OrderType]=b.[OrderType] and a.[LNID]=b.[LNID]))
2.保留一条(这个应该是大多数人所需要的 _)
Delete HZT Where ID Not In (Select Max(ID) From HZT Group By Title)
注:此处保留ID最大一条记录
标签:语句,重复,id,查找,sql,重复记录,where,ID,Select From: https://www.cnblogs.com/Depingblogs/p/17095322.html