怎么删除表中某一列重复的记录?感谢指导

oldlizheng 2006-12-01 03:38:28
要删除表中name字段重复的记录
比如table1,有name,tel等字段,主键为id,自增int
有的记录的name是重复的,现在要删掉重复的,保留任意一个就可以了。

怎么写sql?或用程序实现
...全文
271 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
温飞_会员 2006-12-04
  • 打赏
  • 举报
回复
编程序吧!sql不能一次解决
Juchiyufei 2006-12-04
  • 打赏
  • 举报
回复
感觉你们的问题都比我的要简单,我的问题怎么那么复杂呢?


郁闷呀。
Juchiyufei 2006-12-04
  • 打赏
  • 举报
回复
关注
studenthj 2006-12-01
  • 打赏
  • 举报
回复
select distinct name,tel,id into b
from a
firein 2006-12-01
  • 打赏
  • 举报
回复
delete * from table1
where idnot in
(
select Min(id) from table1
group by name
)
caixia615 2006-12-01
  • 打赏
  • 举报
回复
select distinct name,tel,id from table insert to newtable
冷箫轻笛 2006-12-01
  • 打赏
  • 举报
回复
--保留重复name中id最小的
delete t1 from table1 t1
where exists(select 1 from table1 where name = t1.name and id < t1.id)
friendlyFour 2006-12-01
  • 打赏
  • 举报
回复
Delete(删除重复但保留重复记录中的第一条记录)
--方法一:
--建立測試環境
Create Table A
(id Int,
name Varchar(10),
age Int)
Insert A Select 1, 'w', 18
Union All Select 2, 'e', 17
Union All Select 3, 'w', 18
Union All Select 4, 'e', 17
Union All Select 5, 'e', 17
Union All Select 6, 'q', 16
--測試
Select * from A
Delete from A Where id Not In (Select Min(id) As id from A Group By name,age)
Select * from A
--刪除測試環境
Drop Table A
--結果
/*
--刪除前
idnameage
1w18
2e17
3w18
4e17
5e17
6q16

--刪除後
idnameage
1w18
2e17
6q16
*/
rea1gz 2006-12-01
  • 打赏
  • 举报
回复
delete t
from table1 t
where exists (
select 1 from table1 where name=t.name and id>t.id)

34,590

社区成员

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

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