注:此处“重复”非完全重复,意为某字段数据重复
HZT表结构
ID int
Title nvarchar(50)
AddDate datetime
数据
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:381584252 | 2008-01-19 10:27:30.420 |
8 | QQ:381584252 | 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 |
方法一:
wx_termid为重复字段
select DISTINCT wx_termid from tablename where id=1
方法二:
一。查找重复记录
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最大一条记录