先描述一下情况有表T1,T2,列数是一样的,
T1:
a b c d
1 1 2 2
2 2 4 4
3 3 6 6T2:
a b c d
1 2 3 4
2 2 3 4我想以列a为关联,把T1和T2合并,去掉T1中和T2重复的行,得到以下结果T2T2:
a b c d 
1 2 3 4
2 2 3 4
3 3 6 6
求完整的sql语句...

解决方案 »

  1.   


    create table t1(a int,b int,c int,d int)
    insert into t1
    select 1 ,1 ,2 ,2 union all
    select 2 ,2 ,4 ,4 union all
    select 3 ,3 ,6 ,6
    gocreate table t2(a int,b int,c int,d int)
    insert into t2
    select 1 ,2 ,3 ,4 union all
    select 2 ,2 ,3 ,4
    go
    --a b c d  
    --1 2 3 4
    --2 2 3 4
    --3 3 6 6select *
    from t1 t
    where not exists (select 1 from t2 where a = t.a)
    union all
    select *
    from t2
    order by adrop table t1,t2
    /**************a           b           c           d
    ----------- ----------- ----------- -----------
    1           2           3           4
    2           2           3           4
    3           3           6           6(3 行受影响)
      

  2.   

    你能不能帮我把T2变成结果的样子?这里不用select....用insert
    select *
    from t1 t
    where not exists (select 1 from t2 where a = t.a)
    union all
    select *
    from t2
    order by adrop table t1,t2
      

  3.   


    create table t1(a int,b int,c int,d int)
    insert into t1
    select 1 ,1 ,2 ,2 union all
    select 2 ,2 ,4 ,4 union all
    select 3 ,3 ,6 ,6
    gocreate table t2(a int,b int,c int,d int)
    insert into t2
    select 1 ,2 ,3 ,4 union all
    select 2 ,2 ,3 ,4
    go
    --a b c d  
    --1 2 3 4
    --2 2 3 4
    --3 3 6 6insert into t2
    select *
    from t1 t
    where not exists (select 1 from t2 where a = t.a)select *
    from t2drop table t1,t2/************a           b           c           d
    ----------- ----------- ----------- -----------
    1           2           3           4
    2           2           3           4
    3           3           6           6(3 行受影响)
      

  4.   

    就是把T1和T2合并到T2中去掉重复的以后把结果写到T2中,我想要的这一部分的代码,可能我说得不是太清楚.
      

  5.   


    啥意思?
    insert into t
    select *
    from t1 t
    where not exists (select 1 from t2 where a = t.a)
    union all
    select *
    from t2
    order by a