如何删除同一张表中多行相同的数据但要保留一条?

夏去秋来 2011-12-10 03:58:40
请问下该如何删除同一张表中有多行相同的数据,但是要保留一行的数据,如下表:


ID name cdefine1 cdefine2 cdefine3
1 aa 101 09-08 15:35
2 aa 101 09-09 15:37
3 bb 101 09-09 15:36
4 cc 204 09-12 14:35
5 cc 204 09-13 12:35
6 dd 255 09-05 15:55
7 ee 255 09-06 18:15
8 ee 211 09-14 11:24


现在我要删除表中字段name和cdefine1相同的数据,就是说只要字段name和cdefine1是一样的就删除,但是要保留一条,最终结果如下:

ID name cdefine1 cdefine2 cdefine3
1 aa 101 09-08 15:35
2 bb 101 09-09 15:36
3 cc 204 09-12 14:35
4 dd 255 09-05 15:55
5 ee 255 09-06 18:15
8 ee 211 09-14 11:24


请问该如何实现?先谢谢啦
...全文
275 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rucypli 2011-12-10
  • 打赏
  • 举报
回复
delete a from tb a
where exists (select 1 from tb where name=a.name and cdefine1=a.cdefine1 and id>a.id
快溜 2011-12-10
  • 打赏
  • 举报
回复
delete a from tb a
where exists (select 1 from tb where name=a.name and cdefine1=a.cdefine1 and id>a.id)
--小F-- 2011-12-10
  • 打赏
  • 举报
回复
delete
t
from
tb t
where
exists (select 1 from tb where name=t.name and cdefine1=t.cdefine1 and id>t.id)
  • 打赏
  • 举报
回复
select * from tb t
where not exists(select 1 from tb where name=t.name and cdefine1=t.cdefine1 and cdefine3<t.cdefine3)
AcHerat 2011-12-10
  • 打赏
  • 举报
回复

delete t
from tb t
where exists (select 1 from tb where name=t.name and cdefine1=t.cdefine1 and id>t.id)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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