建立以table,表名为Test_Table,设置字段分别为ID(自增),A,B,C,D,E,然后向里分别插入如下数据:
1,1,1,1,1
1,1,1,1,1
2,2,2,2,2
3,3,3,3,3
4,4,4,4,4
4,4,4,4,4
5,5,5,5,5
5,5,5,5,5
6,6,6,6,6
7,7,7,7,7
如查找重复的数据,则SQL语句如下:
select P.* from Test_Table as P inner join
(SELECT A,B,C,D,E FROM Test_Table
GROUP BY A,B,C,D,E HAVING COUNT(*)>1) as Q
on P.A=Q.A AND P.B=Q.B AND P.C=Q.C AND P.D=Q.D
AND P.E=Q.E,结果如下:
ID A B C D E
1 1 1 1 1 1
2 1 1 1 1 1
5 4 4 4 4 4
6 4 4 4 4 4
7 5 5 5 5 5
8 5 5 5 5 5
如查找查找ID最大的重复记录,代码如下:
select P.* from Test_Table as P inner join
(SELECT A,B,C,D,E FROM Test_Table
GROUP BY A,B,C,D,E HAVING COUNT(*)>1) as Q
on P.A=Q.A AND P.B=Q.B AND P.C=Q.C AND P.D=Q.D
AND P.E=Q.E
WHERE P.ID NOT IN (SELECT MIN(ID) FROM Test_Table
GROUP BY A,B,C,D,E HAVING COUNT(*)>1)
结果如下所示:
ID A B C D E
2 1 1 1 1 1
6 4 4 4 4 4
8 5 5 5 5 5
若删除重复记录中ID值最大的数据,则代码如下:
delete from Test_Table where ID IN (
select P.ID from Test_Table as P inner join
(SELECT A,B,C,D,E FROM Test_Table
GROUP BY A,B,C,D,E HAVING COUNT(*)>1) as Q
on P.A=Q.A AND P.B=Q.B AND P.C=Q.C AND P.D=Q.D
AND P.E=Q.E
WHERE P.ID NOT IN (SELECT MIN(ID) FROM Test_Table
GROUP BY A,B,C,D,E HAVING COUNT(*)>1))
当然,以上代码只是针对表中数据比较少而言,如数据表中存储有百万条以上数据,如删除其中的重复数据,建议还是需要加入临时表进行处理。