删除重复记录,只保留一条

red-fly 2002-11-27 11:07:55
把一个数据库中重复的记录只保留一条而把其他的(有重复的)删除掉,如果没有重复的就不删除,条件是把照某一字段是否重复
一个例子:
表名:tb
字段:
id i_ip i_date i_comeid
1 x.x.x.x 2002.11.2 78
2 xx.x.x.x 2002.11.3 79
3 x.xx.xx.x 2002.11.1 80
4 xx.xx.x.x 2002.11.9 79
5 x.xx.xx.x 2002.11.13 80
6 x.xx.x.x 2002.11.20 79
7 x.xx.x.xx 2002.11.5 83
...
其中,id是自动不重复的,i_date是日期,都不重复,i_comeid是数字,有重复
条件:把i_comeid中相同的部分只保留一个而其他的删除,结果如下:
id i_ip i_date i_comeid
1 ... 2002.11.2 78
2 ... 2002.11.3 79
3 ... 2002.11.1 80
7 ... 2002.11.5 83
...

(说明:i_comeid是78和83的两条记录本来就是单条的,所以不被删除,79和80的有重复的,所以保留了一条,而把其他的删除了。至于重复中删除哪几条都可以,只要保留一条就可以)
这样的sql用一句怎么写?
...全文
30 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
borisnew 2002-11-30
  • 打赏
  • 举报
回复
我觉得好像都不对
amtyuranus 2002-11-29
  • 打赏
  • 举报
回复
delete c.* from tb as c,(select a.* from tb a,(select count(i_comeid),i_comeid from tb group by i_comeid) as b where a.i_comeid=b.i_comeid and b.count(i_comeid) >=2) as d where id<>min(d.id)
可能有点烦看起来,但应该能解决了,我直接写的,可能有点错误,你改一下
蓝天 2002-11-29
  • 打赏
  • 举报
回复
delete from tb where id not in (select min(id) from tb group by i_comeid)
red-fly 2002-11-29
  • 打赏
  • 举报
回复
dcount("id","a","i_comeid="&i_comeid&" and id<"&id)
是什么意思?返回值是一个数字吗?
好象从来都没有见过dcount,书本上也查不到
其中的id是字段,那么a是表名怎么可以写在这里呢?
"i_comeid="&i_comeid&" and id<"&id 这一字符串是什么条件?
amtyuranus 2002-11-27
  • 打赏
  • 举报
回复
同意上面的
奇遇 2002-11-27
  • 打赏
  • 举报
回复
delete from a where dcount("id","a","i_comeid="&i_comeid&" and id<"&id)>0

7,714

社区成员

发帖
与我相关
我的任务
社区描述
Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点。
社区管理员
  • Access
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧