在线咨询
QQ咨询
服务热线
服务热线:13125520620
TOP

如何删除表中的相同记录-数据库

发布时间:2011-11-12 浏览:5023

如何删除表中的相同记录

有一个表,有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))

TAG
软件定制,软件开发,瀚森HANSEN,辽宁,沈阳,抚顺
0
该内容对我有帮助