去除重复行或列的一些sql语句

搜集起来留着以后用吧


①查询结果有两条完全相同的行,用distinct

select distinct * from table(表名) where (条件)

②存在部分字段相同的纪录(但是有有主键id,即唯一键)
    如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组

select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])

③没有唯一键ID

这种情况最复杂

select identity(int1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
drop table newtable
本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/07/03/4576212.html

本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/07/03/4576212.html
如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
原文地址:https://www.cnblogs.com/liqipeng/p/4576212.html