SQL查询小难题请教,高手进!!
我现在有一张表,有些重复的记录,但不是完全重复,比如,姓名相同(都是张山),电话相同(都是123456),但地址有一点不同(A:北京路1号[b]楼[/b]201房,--B:北京路1号201房),以上的2条记录应该是同一个人的.我如何用SQL语句删除其中的一条?多谢大家,现在很急,高手指教!! 你的表难道没有索引的吗? 兄弟,其实你的这个问题这儿早就有答案了啊。
我把ZERRY兄弟的贴子转给你吧。
如何删除表中的相同记录(转)
有一个表,有20万条记录,其中某个字段的各记录值不可重复,现要删除重复记录,如何*作更有效、快速
1,如果有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...) 没有那么复杂,用自连接查询就可以了 也許直接用distinct就可以 哦 ,还是感觉luckdevil 的方法好一点。
条理很清晰 还是luckdevil 的不错 向高手学习
页:
[1]