SQL 刪除相同的記錄

問:
在sqlserver一个表中,有id,name,xm三个字段,共10条记录,记录的id值有重复,但是name,xm没有重复的,现在想将id重复的记录只保留一条,如何写sql语句??
答:
删除重复数据  
   
  一、具有主键的情况  
  a.具有唯一性的字段id(为唯一主键)  
  delect   table    
  where   id   not   in    
  (  
  select   max(id)   from   table   group   by   col1,col2,col3...  
  )  
  group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,  
  那么只要col1字段内容相同即表示记录相同。  
   
  b.具有联合主键  
  假设col1+','+col2+','...col5   为联合主键  
  select   *   from     table   where   col1+','+col2+','...col5   in   (  
      select   max(col1+','+col2+','...col5)   from   table    
  where   having   count(*)>1  
  group   by   col1,col2,col3,col4    
  )  
  group   by   子句后跟的字段就是你用来判断重复的条件,  
  如只有col1,那么只要col1字段内容相同即表示记录相同。  
   
   
  or  
  select   *   from   table     where   exists   (select   1   from   table   x   where   table.col1   =   x.col1   and    
  table.col2=   x.col2   group   by   x.col1,x.col2   having   count(*)   >1)  
   
  c:判断所有的字段  
      select   *   into   #aa   from   table   group   by   id1,id2,....  
      delete   table    
      insert   into   table    
      select   *   from   #aa  
   
  二、没有主键的情况  
   
  a:用临时表实现  
  select   identity(int,1,1)   as   id,*   into   #temp   from   ta  
  delect   #temp    
  where   id   not   in    
  (  
      select   max(id)   from   #   group   by   col1,col2,col3...  
  )  
  delete   table   ta  
  inset   into   ta(...)  
        select   .....   from   #temp  
   
  b:用改变表结构(加一个唯一字段)来实现  
  alter   table   表   add     newfield   int   identity(1,1)  
  delete   表  
  where   newfield   not   in  
  (  
  select   min(newfield)   from   表   group   by   除newfield外的所有字段  
  )  
   
  alter   table   表   drop   column   newfield  

posted on 2008-04-29 11:54 破曉之陽 阅读(320) 评论(1)  编辑 收藏 所属分类: sql

评论

#1楼 [楼主] 2008-04-29 11:56 破曉之陽      

第一个题有二张表A,B。A表有二个列。NAME VARCHAR(20)AGE INT 。B表也有二列 NAME VARCHAR(20)GRADE INT
A表中有三条记录,B表中有四条记录。问现在要用一条SQL语句根据A表中AGE字段把B表中的GRADE字段进行更新。条件为A表NAME和B表NAME相等。同样。B表中与A表没有相同NAME的记录不变。
MSSQL2000
第二问题。一个表中有二个列就以上A表为例。现在A表中有多例相同的记录。现在要把相同的记录删除只留一条。没有相同记录的记录不变。A表中可能四条xiao的记录。二条li的记录。一条zhang的记录
第一個問題
update B
set grade = A.age
from B,A 
where B.name = A.name
第二個問題:

create table a
(
NAME 
VARCHAR(20),
AGE 
INT
)
insert into a
select 'xiao',21
union all
select 'xiao',22
union all
select 'xiao',23
union all
select 'xiao',23
union all
select 'li',22
union all
select 'li',22
union all
select 'zhang',22

select * from a

delete A from A t where age not in (select max(age) from A where name = t.name)

--临时表
 select   identity(int,1,1)   as   id,*   into   #temp   from   a  
delete   #temp    
  
where   id   not   in    
  (  
      
select   max(id)   from   #temp   group   by   name,age 
  )  
delete  a from a
insert   into   a(name,age)  
        
select  name,age   from   #temp  

select * from a

drop table  #temp
drop table a


/*NAME                 AGE         
-------------------- ----------- 
xiao                 21
xiao                 22
xiao                 23
xiao                 23
li                   22
li                   22
zhang                22


NAME                 AGE         
-------------------- ----------- 
xiao                 23
li                   22
zhang                22
*/

  回复  引用  查看    

导航

留言簿

随笔分类(55)

随笔档案(81)

文章分类(2)

文章档案(9)

收藏夹(102)

javascript庫

常去的网站

最新随笔

搜索

积分与排名

最新评论

阅读排行榜