SQL语句 如何实现 删除表中重复的记录,但是要要保留下来一条记录。

来源:百度知道 编辑:UC知道 时间:2024/07/02 12:17:58
id name age
1 张三 19
2 李四 30
3 王五 34
3 王五 34
用一条sql语句实现表如下:
id name age
1 张三 19
2 李四 30
3 王五 34
保留一条记录,不要全部删除!
delete from mmf
where matid not in(select min(matid) from mmf group by code )

这是别人给的答案 , 其实这样的表设计就有问题 设置个primary key 就可以了~~~~

declare @table table(id varchar(20),name varchar(50),age int)
insert into @table(id,name,age)
select distinct * from t
delete from t
insert into t(id,name,age)
select id,name,age from @table

可以吗?我还期待答案呢

期待一下

select distinct * into tablename2 from tablename1
drop tablename1
update sysobjects s et name='tablename2' where name='tablename1'