SQL重复记录处理(查找,过滤,删除)
ID int
Title nvarchar(50)
AddDate datetime
数据 www.2cto.com
ID Title AddDate
1 台州站长论坛1 2008-01-17 10:27:24.827
2 台州站长论坛1 2008-03-17 10:27:21.780
3 台州站长论坛2 2008-05-17 10:27:30.420
4 台州站长论坛2 2008-09-17 10:27:30.420
5 台州站长论坛3 2008-01-20 10:27:30.420
6 台州站长论坛3 2008-01-15 10:27:30.420
7 QQ:147526645 2008-01-19 10:27:30.420
8 QQ:147526645 2008-01-18 10:27:30.420
9 Email:hztgcl1986@163.com 2001-01-17 10:27:30.420
10 Email:hztgcl1986@163.com 2009-01-17 10:27:30.420
11 HZT 2005-01-17 10:27:30.420
12 1314521 2006-01-17 10:27:30.420
13 LoveCY 2007-01-17 10:27:30.420
14 19861108 2008-03-17 10:27:30.420
15 CY 2008-02-17 10:27:30.420
一。查找重复记录
1。查找全部重复记录
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
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)
2。保留一条(这个应该是大多数人所需要的 ^_^)
Delete HZT Where ID Not In (Select Max(ID) From HZT Group By Title)
注:此处保留ID最大一条记录.