A,B表的结构一样,主键是X
想生成C表,包含A表的全部记录,和B表中不与A表重复的记录.

解决方案 »

  1.   

    SELECT * FROM TA
    UNION 
    SELECT * FROM TB
      

  2.   

    select distinct * 
    from
    (
      select * from A
      union all
      select * from B
    ) t
      

  3.   

    select *
    from a
    union all
    select *
    from b as t
    where not exists(select 1 from a where x = t.x)
      

  4.   

    这个不行,没有包含TA的全部记录SELECT * FROM TA
    UNION 
    SELECT * FROM TB
      

  5.   

    select * from A
    union all
    (
      select * from B b where not exists (select * from A where X=b.x)
    )
      

  6.   

    select t.* 
    into C
    from
        (select * from A union all select * from B where not exists(select 1 from A where X=B.X)) t
      

  7.   

    select * from tba
    union 
    select * from tbb where tbb.x not in (select distinct x from tba)
      

  8.   


    declare @tablea table (X varchar(12))
    insert into @tablea
    select '4' union all
    select '5' union all
    select '6' union all
    select '7'--select * from @tableadeclare @tableb table (X varchar(12))
    insert into @tableb
    select '5' union all
    select '8' union all
    select '9'--select * from @tablebdeclare @tablec table (X varchar(12))insert into @tablec
    select distinct * 
    from
    (
      select * from @tablea
      union all
      select * from @tableb
    ) tselect * from @tablec/*
    X
    ------------
    4
    5
    6
    7
    8
    9
    */
      

  9.   

    declare @tablea table (X varchar(12))
    insert into @tablea
    select '4' union all
    select '5' union all
    select '6' union all
    select '7'
    declare @tableb table (X varchar(12))
    insert into @tableb
    select '5' union all
    select '8' union all
    select '9'select * from @tablea
    UNION
    select * from @tableb
    /*X            
    ------------ 
    4
    5
    6
    7
    8
    9
    */
      

  10.   

    这句是不是有问题啊?当X取一个A、B表都有的,不就没有记录插入C表了吗?
      

  11.   

    --不好意思,是我理解错了,应该这样理解:
    select t.* 
    into C 
    from 
        (select * from A union all /**/select * from B where not exists(select 1 from A where X=B.X)/**/    ) t 
    --应该是2个/**/之间的和前面的UNION ALL
      

  12.   


    SELECT * INTO C
    (
    SELECT * FROM TA
    UNION
    SELECT * FROM TB
    ) AS T
      

  13.   

    select t.* 
    into C 
    from 
        (select * from A union all /**/select * from B where not exists(select 1 from A where X=B.X)/**/    ) t 
      

  14.   

    SQL codeselect * from A 
    union all 

      select * from B b where not exists (select * from A where X=b.x) 
    ) 关注