写sql语句

tlei9 2003-08-21 02:41:10
现在有这样一个要求,写一个sql 语句把一个表中重复的记录删除,重复的记录只留一条,(假设表中存在重复的记录)
...全文
23 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fmdsaco 2003-08-21
  • 打赏
  • 举报
回复
use 数据库
alter table 表 add rownum int identity(1,1)
go
delete from 表 where rownum not in (select min(rownum ) from 表group by 重复字段名)
go
alter table 表 drop column rownum
go
pengdali 2003-08-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

或:

select distinct * into #temp from 表
truncate table 表
insert 表 select * from #temp
drop table #temp
愉快的登山者 2003-08-21
  • 打赏
  • 举报
回复
select distinct * into #t from table1
truncate table table1
go
insert table1 select * from #t
go


愉快的登山者


◢◣◢◣◢◣

34,575

社区成员

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

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