如何删除表中的相同记录

王朝other·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

如何删除表中的相同记录

有一个表,有20万条记录,其中某个字段的各记录值不可重复,现要删除重复记录,如何操作更有效、快速?

----------------------------------------------------------------

回复人: CrazyFor(吃素的鼹鼠) ( ) 信誉:262 2003-2-20 17:39:11 得分:30

如果有ID字段,就是具有唯一性的字段

delect table where id not in (

select max(id) from table group by col1,col2,col3...

)

group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

2,如果是判断所有字段也可以这样

select * into #aa from table group by id1,id2,....

delete table

insert into table

select * from #aa

3,没有ID的情况

select identity(int,1,1) as id,* into #temp from tabel

delect # where id not in (

select max(id) from # group by col1,col2,col3...)

delect table

inset into table(...)

select ..... from #temp

col1+','+col2+','...col5 联合主键

select * from table where col1+','+col2+','...col5 in (

select max(col1+','+col2+','...col5) from table

where having count(*)>1

group by col1,col2,col3,col4

)

group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

2,

select identity(int,1,1) as id,* into #temp from tabel

select * from #temp where id in (

select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)

-----------------------------------------------------------------

回复人: 19191919(红围巾) ( ) 信誉:101 2003-2-20 17:39:20 得分:20

---先找出重复记录 插入到临时表中

insert into #temp

select col1,col2.. from table

group by col1,col2..

having count(*)>1

---删除这些重复记录

delete table from #temp where table.col1=#temp.col1 and table.col2=#temp.col2...

--再将临时表的数据反添回来

insert into table

select col1,col2 ...from #temp

-----------------------------------------------------------------

回复人: lang_ren(浪人) ( ) 信誉:100 2003-2-20 19:06:55 得分:20

alter table yourtable add rownum int identity(1,1)

go

delete from yourtable where rownum not in (select min(rownum ) from yourtable group by 你重复的字段名)

go

alter table yourtable drop column rownum

go

-------------------------------------------------------------------

回复人: pengdali(大力 V3.0) ( ) 信誉:541 2003-2-20 19:11:42 得分:20

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

---------------------------------------------------------------------

回复人: cpflj(飞鹏) ( ) 信誉:100 2003-2-21 11:16:46 得分:5

SELECT * FROM 表 WHERE (相同的字段名 IN (SELECT 相同的字段名 FROM 表 GROUP BY 相同的字段名 HAVING COUNT(相同的字段名) > 1))

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航