有两个表a、b,字段也相同,其中a、b表中有相同的数据,请问怎么将a、b表中的数据全部插入到c表中(相同的也插入),然后又怎么删除相同的数据,相同的数据只需要一行就可以了。

解决方案 »

  1.   


    insert into [c] 
     select * from [a]
     union all
     select * from [b];-- 不知道 lz 说的相同是完全相同,还是某些字段相同。
    -- 如果是前者,则将上面语句中的 union all 换成 union。
    -- 如果是后者,可以参考精化贴中的去重语句。例如,
    delete t from [c] t where exists (select * from [c] where [相同的字段]=t.[相同的字段] and [某一不相同的字段]>t.[某一不相同的字段])
      

  2.   

    --使用union合并a,b两表,然后去掉重复.
    insert into c
    select * from a 
    union 
    select * from b
      

  3.   

    insert into c
    select distinct *
    from ( select * from a  
    union all select * from b )t