Sql去重

杀马特。学长 韩版系。学妹 提交于 2019-12-05 06:29:58

原文链接:https://www.jianshu.com/p/4a10aac1293c

1.存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉
例子:

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

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

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

3.没有唯一键ID
这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:

例子:

       select identity(int,1,1) as id,* into newtable(临时表) from table

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

       drop table newtable
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!