急求按一个字段删除重复记录的sql

TopFans 2007-08-31 03:15:48
表 test1, 字段为id int, name varchar
name重复,id主键,求删除重复的name字段的sql语句
很急啊,试了半天没试出来。。
...全文
337 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
paoluo 2007-09-07
  • 打赏
  • 举报
回复
...
gxqianqin 2007-08-31
  • 打赏
  • 举报
回复
謝謝,學習了
TopFans 2007-08-31
  • 打赏
  • 举报
回复
恩 偶菜啊
唉, 每天在公司混日子~
---涛声依旧--- 2007-08-31
  • 打赏
  • 举报
回复
樓上的解答可以幫助樓主解決問題了
但提醒樓主,以后建表時不要使用系統關鍵字作為字段名,如: name 等。
dawugui 2007-08-31
  • 打赏
  • 举报
回复
學習了,我也遇到過差不多的問題
但有點區別
我的有幾個字段
比如,表名FileName
字段FileId ,FileName,FileNode,FileText
這幾個字段都不是關鍵字,我的要求是:
這幾個字段都可能有完全相同的記錄,我隻要保留其中的隨便一條,請問怎麼寫
當初我是用寫了個存儲過程,並用了遊標解決的


這幾個字段都可能有完全相同的記錄,我隻要保留其中的隨便一條,請問怎麼寫
如果还有一个能区分大小(這幾個字段除外)id1
delete from tb where id1 not in (select min(id1) from tb group by 這幾個字段)
如果没有一个能区分大小,则只能使用临时表.
select id2 = identity(int,1,1) , * into temp from tb
delete from tb where id2 not in (select min(id2) from tb group by 這幾個字段)

gxqianqin 2007-08-31
  • 打赏
  • 举报
回复
學習了,我也遇到過差不多的問題
但有點區別
我的有幾個字段
比如,表名FileName
字段FileId ,FileName,FileNode,FileText
這幾個字段都不是關鍵字,我的要求是:
這幾個字段都可能有完全相同的記錄,我隻要保留其中的隨便一條,請問怎麼寫
當初我是用寫了個存儲過程,並用了遊標解決的
xray2005 2007-08-31
  • 打赏
  • 举报
回复
学习
lost_queen 2007-08-31
  • 打赏
  • 举报
回复
呵呵~本来就是一句话,这里很多高手的~哈哈
学习
TopFans 2007-08-31
  • 打赏
  • 举报
回复
不是吧 大家都一句话就搞定了。。
我faint~~
SoftwKLC 2007-08-31
  • 打赏
  • 举报
回复
---保留相同Name,id最大那行
Delete A From Test1 As A Where Exists
(Select 1 From Test1 Where Name=A.Name And id>A.id)
---保留相同Name,id最小那行
Delete A From Test1 As A Where Exists
(Select 1 From Test1 Where Name=A.Name And id<A.id)
---只要Name有重复的,就将其全部删除
Delete A From Test1 As A Where Exists
(Select 1 From Test1 Where Name=A.Name Group By Name Having Count(1)>1)
SoftwKLC 2007-08-31
  • 打赏
  • 举报
回复
---删除重复,保留相同Name,id最大那行
Delete A From @Test1 As A Where Exists
(Select 1 From @Test1 Where Name=A.Name And id>A.id)
---删除重复,保留相同Name,id最小那行
Delete A From @Test1 As A Where Exists
(Select 1 From @Test1 Where Name=A.Name And id<A.id)
dawugui 2007-08-31
  • 打赏
  • 举报
回复
--保留一条
delete from test1 where id not in (select min(id) from test1 group by name)

--删除全部
delete from test1 where name in (select name from test1 group by name having count(*) > 1)

dawugui 2007-08-31
  • 打赏
  • 举报
回复
表 test1, 字段为id int, name varchar
name重复,id主键,求删除重复的name字段的sql语句
很急啊,试了半天没试出来。。

delete from test1 where id not in (select min(id) from test1 group by name)
lxzm1001 2007-08-31
  • 打赏
  • 举报
回复
如果是要删除所有重复制的
delete test1 where name in(select name from test1 group by name having count(*)>1)
lxzm1001 2007-08-31
  • 打赏
  • 举报
回复
delete test1 where id not in(select min(id) from test1 group by name)
paoluo 2007-08-31
  • 打赏
  • 举报
回复
--如果是隨便保留哪條數據
Delete From test1 Where id Not In (Select Min(ID) From test1 Group By name)

--或者
Delete From test1 Where id Not In (Select Max(ID) From test1 Group By name)

34,590

社区成员

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

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