SQL Server怎样用一条SQL语句删除重复的行。

juhwali 2003-06-26 06:54:01
因为数据表没有主键,有时会出现完全相同的数据,如何用一条SQL删除所有的重复行,只保留其中的一行。
...全文
359 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wycg_cnh20 2003-06-27
  • 打赏
  • 举报
回复
一条语句无法实现,推荐使用大力的
longji 2003-06-27
  • 打赏
  • 举报
回复
select * into #temp from table0--你的原始表名
where 1=2
insert into #temp select distinct * from table0
delete table0
insert into table0 select * from #temp
drop table #temp

to longji(龙寂) :
‘ select * into #temp from table0--你的原始表名
where 1=2
insert into #temp select distinct * from table0 ’

能不能写成: select distinct * into #temp from table0 ?
可以写成select distinct * into #temp from table0
但我想可能相对来说,前面那样写会快一些,当然用truncate table0 的删除速度更快
shierre 2003-06-27
  • 打赏
  • 举报
回复
select distinct * into #temp from
truncate table
insert 表 select * from #temp
drop table #temp
skywebnet 2003-06-27
  • 打赏
  • 举报
回复
delete table
from table a
where a.field=field
shb_zj 2003-06-27
  • 打赏
  • 举报
回复
推荐使用大力的:
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 ---去掉加的列
饮水需思源 2003-06-27
  • 打赏
  • 举报
回复
select distinct * into 新表名 from 旧表名
再删除旧表名,将新表名改成旧表名
pengdali 2003-06-26
  • 打赏
  • 举报
回复
或:

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-06-26
  • 打赏
  • 举报
回复
select distinct * into #temp from 表 --去除重复,并把数据拿出

truncate table 表 --删掉所有数据

insert 表 select * from #temp --插入临时表数据

drop table #temp --删掉临时表
histock 2003-06-26
  • 打赏
  • 举报
回复
to hjb111(苦行僧) :

' select identity(int,1,1) as T,* into #temp from yourtable
delete yourtable where id not in(select F,id from #temp group by F,id )
drop #temp ' 能不能注释下,我是初学者,看不太懂。 谢谢
histock 2003-06-26
  • 打赏
  • 举报
回复
to longji(龙寂) :
‘ select * into #temp from table0--你的原始表名
where 1=2
insert into #temp select distinct * from table0 ’

能不能写成: select distinct * into #temp from table0 ?
longji 2003-06-26
  • 打赏
  • 举报
回复
select * into #temp from table0--你的原始表名
where 1=2
insert into #temp select distinct * from table0
delete table0
insert into table0 select * from #temp
drop table #temp
hjb111 2003-06-26
  • 打赏
  • 举报
回复
select identity(int,1,1) as T,* into #temp from yourtable
delete yourtable where id not in(select F,id from #temp group by F,id )
drop #temp
sdhdy 2003-06-26
  • 打赏
  • 举报
回复
--example
create table aaa(F1 int,F2 varchar(10))
insert aaa select 1,'hdy'
insert aaa select 2,'lb'
insert aaa select 1,'hdy'
insert aaa select 3,'xzn'
insert aaa select 4,'zrp'
insert aaa select 1,'hdy'
insert aaa select 2,'lb'
insert aaa select 1,'hdy'
insert aaa select 1,'hdy'
insert aaa select 3,'xzn'
insert aaa select 1,'hdy'
insert aaa select 4,'zrp'

select identity(int,1,1) F0 ,* into bbb from aaa
delete bbb where f0 not in (select min(F0) from bbb group by F1,F2)

delete aaa
insert aaa select F1,F2 from bbb
select * from aaa
select * from bbb
drop table bbb
xiehua822 2003-06-26
  • 打赏
  • 举报
回复
create table #表1(字段和你的表一样)
insert into #表1 select destinct * from 你的表 group by 需要分组的字段名
delete from 你的表 where 字段
insert into 你的表 select * from #表1
97866 2003-06-26
  • 打赏
  • 举报
回复
可能吗?

34,587

社区成员

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

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