请问怎样用SQL脚本把表中重复的数据删掉?

Yasom 2003-10-21 09:26:55
请问怎样用SQL脚本把表中重复的数据删掉?例如有三条数据分别是:1000,1001,1000。我的要求是只要一个1000,而另一个删掉。
...全文
65 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvcheng606717 2003-10-21
  • 打赏
  • 举报
回复
select distinct 字段名 into 临时表 from tablename

delete from tablename

select * from 临时表 into tbalename from 临时表

drop table 临时表
pengdali 2003-10-21
  • 打赏
  • 举报
回复
或:

删除重复的,只留一条:

alter table 表 add newfield int identity(1,1)

delete 表
where newfield not in(
select min(newfield) from 表 group by 除newfield外的所有字段
)

alter table 表 drop column newfield
pengdali 2003-10-21
  • 打赏
  • 举报
回复
select distinct * into #temp from 表
truncate table 表
insert 表 select * from #temp
drop table #temp
52vc 2003-10-21
  • 打赏
  • 举报
回复
先建一个视图
select count(*) from yourtable group by ID
找出有两条以上记录的数据
再有条件删除
friendliu 2003-10-21
  • 打赏
  • 举报
回复
select distinct 字段名 into #a from tablename
delete from tablename
select * into tbalename from #a
drop table #a
go

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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