查找重复数据?

mrking 2007-10-24 03:53:20
现有一表temp其中一列如下:
name
A-H1-047
A-H1-002
A-H1-003
A-H1-047
A-H1-047
A-H1-047

想查找出name列中有重复的数据A-H1-047并删除该行,得到如下:

name
A-H1-047
A-H1-002
A-H1-003
...全文
145 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
gbys 2007-10-25
  • 打赏
  • 举报
回复
mark
ShaRoom 2007-10-25
  • 打赏
  • 举报
回复
不含distinct的sql.
楼上的很好:
delete from table where name in(select name from table group by name having count(name) >1).
SQLnewlearner 2007-10-24
  • 打赏
  • 举报
回复
delete from table where name in(select name from table group by name having count(name)>1)
wuya8115 2007-10-24
  • 打赏
  • 举报
回复
select distinct name into test from temp
truncate table temp
insert into temp select * from test
yuemb369 2007-10-24
  • 打赏
  • 举报
回复

select 'a' as [name] into #temp
insert into #temp
select 'b'
union all
select 'b'
union all
select 'c'


delete from #temp where name in (select name from #temp group by name having count(name)>1) --把这句中的#temp换成你自己的temp就可以了,其余的语句为测试语句

select * from #temp
nobody@noone 2007-10-24
  • 打赏
  • 举报
回复
不是先group by 有用的字段然后用not in delete么
我想这个表应该有个主键的吧,我用id代替
delete from temp where id not in(select max(id),name from temp group by name )
IT孤儿 2007-10-24
  • 打赏
  • 举报
回复
--查找所有重复数据
select * from temp where name in (select name from temp group by name having count(name)>1)
要删除就是
select distinct name into test from temp
delete from temp
insert into temp select * from test

fcuandy 2007-10-24
  • 打赏
  • 举报
回复
1,若查找含有重复行的name
select name from tb group by name having count(*)>1

2,若查找重复name的记录,只保留一条
select distinct name into # name from tb
truncate table tb
insert tb select name from #
drop table #
mrking 2007-10-24
  • 打赏
  • 举报
回复
select distinct name from temp
这句没有查找出重复数据的功能,有没有其他的办法啊
vchao13 2007-10-24
  • 打赏
  • 举报
回复
楼上正解。。。

select distinct name into test from temp
truncate table temp
insert into temp select * from test
lass_name 2007-10-24
  • 打赏
  • 举报
回复
楼上正解啊!!
dawugui 2007-10-24
  • 打赏
  • 举报
回复
--如果是删除,将查询出来的数据放一临时表,清空原来的表,然后将数据从临时表中导回来
select distinct name into test from temp
delete from temp
insert into temp select * from test
dawugui 2007-10-24
  • 打赏
  • 举报
回复
--如果是查询
select distinct name from temp

34,588

社区成员

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

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