日期:2014-05-19  浏览次数:20685 次

如何更新某两个字段重复值10次以上的记录
表tabel1中有多个字段,其它remoteip和clickdate字段重复10次以上的,只保留十条记录的flag   =   0其它的flag字段值更新为1请问要怎么写呢?
比如
id           remoteip         clickdate       flag       other
1             127.0.0.1         2007-8-1           0             itads23
1             127.0.0.1         2007-8-1           0             itads232
1             127.0.0.1         2007-8-1           0             itads23
1             127.0.0.1         2007-8-1           0             itads3232
1             127.0.0.1         2007-8-1           0             itads23
1             127.0.0.1         2007-8-1           0             itads32
1             127.0.0.1         2007-8-1           0             itads23
1             127.0.0.1         2007-8-1           0             itads
1             127.0.0.1         2007-8-1           0             itads
1             127.0.0.1         2007-8-1           0             itads
1             127.0.0.1         2007-8-1           0             itads
1             127.0.0.1         2007-8-1           0             itads
1             127.0.0.1         2007-8-1           0             itads
1             127.0.0.1         2007-8-1           0             itads
1             127.0.0.2         2007-8-1           0             itads
1             127.0.0.2         2007-8-1           0             itads

这个表如何写SQL可以使得127.0.0.1   2007-8-1只保留10条flag   =0   其它的记录flag记录更新为1呢?

------解决方案--------------------
怎么好多完全相同的纪录,是不是应该id不同的?

如果id不同:
update a
set flag=1
from tabel1 a
where flag=0
and (select count(*) from table1 where remoteip=a.remoteip and clickdate=a.clickdate and flag=0 and id <=a.id)> 10

如果id相同,估计怎么改都会出错,提示健值不足