首页 | CG软件 | CG信息 | CG教程 | 三维作品 | 设计理论 | CG加油站 | 设计鉴赏 | 社区图库 | Flash | 专题 | 顶客 | 论坛 | 博客 | 导航 | 下载 | 高级搜索
RSS
热门: 3D Total  RPC  Evermotion  效果图  创意  装修  优秀作品  Vray  艺术  欣赏

如何删除表中的相同记录

来源:不详 作者:佚名 时间:2007-07-05 点击:
记录   相同   删除   如何删除表中的相同记录

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


本文地址:http://www.cg3000.com/html/cgTutorials/Website/SQL/20070705/ruheshanchubiaozhongdexiangtongjilu_16567.shtml
上一篇:实用技巧:用MYSQL记录记录网站未知错误的发生
下一篇:解决问题: sql server 2000 企业管理器打不开了
关键字:迅雷 快车 Baidu 删除 相同
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
图腾CG联盟免责声明:本网站转载纯粹出于为网民传递更多信息之目的,无任何商业目的。如本网站转载稿件涉及版权等问题,请作者速来函admincg3000@163.com联系,在此,我们首先向您表示诚挚的歉意,并将尽快予以改正。本网站上发表的内容只代表作者本人之观点,本网站只出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性。
赞助广告