注:此處「重複」非徹底重複,意爲某字段數據重複 數據庫
HZT表結構教程
ID intit
Title nvarchar(50)table
AddDate datetimedate
數據select
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 gc
一。查找重複記錄im
1。查找所有重複記錄數據
Select * From 表 Where 重複字段 In (Select 重複字段 From 表 Group By 重複字段 Having Count(*)>1)datetime
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最大一條記錄
有一個表的結構是這樣的
id bookname cbs zz
001 asp大全 清華大學出版社 田正平
002 數據庫系統教程 高等教育出版社 周奧英
003 asp大全 清華大學出版社 田正平
如今我想作一個查詢,要查找出的字段以下
bookname, cbs, zz
按bookname字段過濾記錄,但要顯示多個字段
不知道上面的回答怎麼樣?不過下面的語句已經通過千錘百煉,呵呵。試試吧
select bookname,cbs,zz from table where id in(select min(id) from table group by bookname)
select t1.bookname,t1.zz,t1.cbs from table t1
join ( select min(id) as id,bookname from table
group by bookname ) as t2
on t1.bookname = t2.bookname and t1.id = t2.id
SELECT t1.CardID, t1.CardValue, t1.RecordID FROM eConsumeData t1 INNER JOIN (SELECT Max(recordid) AS recordid, cardid FROM eConsumeData GROUP BY cardid) t2 ON t1.CardID = t2.cardid AND t1.RecordID = t2.recordid ORDER BY t2.cardid