SQL笔记---删除重复数据,保留第一个

来源:互联网 发布:2016中国网络影视峰会 编辑:程序博客网 时间:2024/06/03 03:08
删除重复数据,保留第一个

这里以删除 t_organization 的重复字段 为例:
delete from t_organization
where   name in (select name    from t_organization group by name      having count(name) > 1) 

and   id not in (select min(id) from t_organization group by name     having count(name)>1) 


只需将 t_organization 替换成 对应表名 ,name 换成 判断重复 的字段。


如何报“对t_organization无效”错,则使用下面语句

将 databaseName换成你的数据库名

use[databaseName]

go

delete from t_organization

where   name in (select name    from t_organization group by name      having count(name) > 1) 

and   id not in (select min(id) from t_organization group by name     having count(name)>1) 

go
0 0