删除表中某字段重复记录

来源:互联网 发布:杭州牛盾网络 编辑:程序博客网 时间:2024/03/29 19:32

如题:

 

exp 表结构如下

 

id           value

1              a

2              b

3              c

4              d

5              a

6              b

 

 

目的是做到value列的唯一,那么必须删除掉 两个a,b中的重复列。方法如下:

 

这个是筛选出不重复的行

select * from exp as tmp where id  in ( select top 1 id from exp where tmp.value= exp.value)

 

or

 

select * from exp  where id in ( select max(id) from exp group by value having count(value) >1)

 

删除重复的行

 

delete  from exp  where id not  in ( select max(id) from exp group by value having count(value) >1)

原创粉丝点击