RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
六种SQLServer删除重复行的方法

SQL Server删除重复行是我们最常见的操作之一,下面就为您介绍六种适合不同情况的SQL Server删除重复行的方法,供您参考。

创新互联公司专注于鸡西梨树网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供鸡西梨树营销型网站建设,鸡西梨树网站制作、鸡西梨树网页设计、鸡西梨树网站官网定制、微信平台小程序开发服务,打造鸡西梨树网络公司原创品牌,更为您提供鸡西梨树网站排名全网营销落地服务。

1.如果有ID字段,就是具有***性的字段

 
 
 
  1. delect   table   where   id   not   in   (     
  2.  
  3. select   max(id)   from   table   group   by   col1,col2,col3...     
  4. )     

group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

2. 如果是判断所有字段也可以这样

 
 
 
  1. select   *   into   #aa   from   table   group   by   id1,id2,....     
  2. delete   table       
  3. insert   into   table       
  4. select   *   from   #aa    

3. 没有ID的情况

 
 
 
  1. select   identity(int,1,1)   as   id,*   into   #temp   from   tabel     
  2. delect   #   where   id   not   in   (     
  3. select   max(id)   from   #   group   by   col1,col2,col3...)     
  4. delect   table     
  5. inset   into   table(...)     
  6. select   .....   from   #temp    

4. col1+','+col2+','...col5 联合主键

 
 
 
  1. select   *   from     table   where   col1+','+col2+','...col5   in   (     
  2. select   max(col1+','+col2+','...col5)   from   table       
  3. where   having   count(*)>1     
  4. group   by   col1,col2,col3,col4       
  5. )    

group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

5.

 
 
 
  1. select   identity(int,1,1)   as   id,*   into   #temp   from   tabel     
  2. select   *   from     #temp   where   id   in   (     
  3. select   max(id)   from   #emp   where   having   count(*)>1   group   by   col1,col2,col3...)    

6.

 
 
 
  1. select   distinct   *   into   #temp   from   tablename       
  2. delete   tablename       
  3. go     
  4. insert   tablename   select   *   from   #temp   Sqlclub   
  5. go     
  6. drop   table   #temp  

以上就是SQL Server删除重复行的方法介绍。

【编辑推荐】

SQL Server创建表语句介绍

SQL Server创建数据库的命令

SQL SERVER连接失败的解决方案

带您了解SQL Server游标

SQL Server权限的分配管理


当前名称:六种SQLServer删除重复行的方法
网站链接:http://www.jxjierui.cn/article/copjsde.html