求一SQL语句实现数据表之间的去重复项如有一表AT  中有四个字段a  b  c  d  其中 a字段,b字段,c字段为联合主键
数据如下a  b   c    d
1  11  22   aa
1  22  22   bb
1  33  22   cc
2  11  22   dd有另一张表AT_Temp临时表
也有四个个字段a  b  c  d  其中 a字段,b字段,c字段为联合主键
数据如下
a  b   c   d
1  22  22  bb
1  22  33  ff
2  11  44  gg请问如何实现AT_Temp表中与AT表去掉重复记录并将不重复数据插入到AT表中我如道如果只有一个主键 a 的话用insert into AT select * from A_Temp from a not in (select a from AT) 可以做出来,但当他有三个主键
后我在后面中  and b not in (select b from AT) ...  是不可行的,并且效率低,求一好的SQL

解决方案 »

  1.   


    insert   into   AT   
    select   *   from   A_Temp   from   a 
    where not exists(select 1 from at b where a.a=at.a and a.b=at.b and a.c=at.c)
      

  2.   

    select at_temp.* from at_temp where not exists(select 1 where at_temp.a = at.a and at_temp.b = at.b and at_temp.c = at.c)
      

  3.   

    insert   into   AT   
    select   *   from   A_Temp   from   a 
    where not exists(select 1 from at b where a.a=b.a and a.b=b.b and a.c=b.c)
      

  4.   

    修正一楼的insert   into   AT   
    select   *   from   A_Temp a
    where not exists(select 1 from at b where a.a=b.a and a.b=b.b and a.c=b.c)
      

  5.   

    修正2楼的select at_temp.* from at_temp where not exists(select 1 from at where at_temp.a = at.a and at_temp.b = at.b and at_temp.c = at.c)
      

  6.   


    insert   into   AT   
    select   *   from   A_Temp   from   a 
    where not exists(select 1 from at where a.a=at.a and a.b=at.b and a.c=at.c)
      

  7.   

    SELECT 
        name, 
        classid, 
        counts, 
        classname 
    FROM 
        merchant 
    WHERE 
        counts in 
        ( 
        SELECT 
            max(counts) 
        FROM 
            merchant 
        GROUP BY 
            classname 
        ) 
    ORDER BY 
        counts asc 
      

  8.   

    匆忙中漏写表,现在才发现.多谢海阔天空.select at_temp.* from at_temp where not exists(select 1 from at where at.a = at_temp.a and at.b = at_temp.b and at.c = at_temp.c)