晕死,上面的不整齐,重新画一个
T3:
F1     F2    F3    F4         F5           F6      F7
100    5     7     1       1            NULL   NULL
100    5     9     NULL       0(or NULL)    0       1
101    5     6     1       1             1      1
101    1     1     NULL       0(or NULL)     0       1
200    1     1     1          1            NULL    NULL
234    3     4     1          1            NULL    NULL
339    4     6     NULL       0(or NULL)     0       1

解决方案 »

  1.   

    select * into t3 from (
    select F1,F2,F3,F4,F5,0 F6,0 F7 from T1
    union all
    select F1,F2,F3,0 F4,0 F5,F6,F7 from T2
    ) tem order by f1select * from t3
      

  2.   

    或:select * into t3 from (
    select F1,F2,F3,F4,F5,0 F6,0 F7,0 flag from T1
    union all
    select F1,F2,F3,0 F4,0 F5,F6,F7,1 flag from T2
    ) tem order by f1,flagselect * from t3
      

  3.   

    create table T1(F1 int,     F2 int,      F3 int,     F4 int,    F5 int)
    insert into t1 select 100,     5,         7,        1,       1 
    union all select 101,     5,        6,        1,       1
    union all select 200,     1,        1,        1,       1
    union all select 234,     3,        4,        1,       1
    create table T2(F1 int,    F2 int,      F3 int,      F6 int,   F7 int)
    insert into t2 select 100,    5,         9,         0,      1
    union all select 101,    1,         1,         0,      1
    union all select 101,    5,          6,         1,      1
    union all select 339,   4,         6,         0,      1
    F1     F2    F3    F4         F5           F6      F7
    100    5     7     1       1            NULL   NULL
    100    5     9     NULL       0(or NULL)    0       1
    101    5     6     1       1             1      1
    101    1     1     NULL       0(or NULL)     0       1
    200    1     1     1          1            NULL    NULL
    234    3     4     1          1            NULL    NULL
    339    4     6     NULL       0(or NULL)     0       1select F1, F2, F3, F4, F5,0 as f6,0 as f7
    from t1
    union all
    select F1, F2, F3,0 as f4,0 as f5,  F6, F7
    from t2
    order by 1
      

  4.   

    Transact-SQL 参考  
    UNION
    将两个或更多查询的结果组合为单个结果集,该结果集包含联合查询中的所有查询的全部行。有关更多信息,请参见 SELECT。©1988-2000 Microsoft Corporation。保留所有权利。
      

  5.   

    有些问题,能改下吗?
    这样出来的结果是
    F1     F2    F3    F4         F5           F6      F7
    100    5     7     1       1            NULL   NULL
    100    5     9     NULL       0(or NULL)    0       1
    101    5     6     0       0             1      1------>
    101       5         6         1             1                          0           0------>这两行能不能合并?101    1     1     NULL       0(or NULL)     0       1
    200    1     1     1          1            NULL    NULL
    234    3     4     1          1            NULL    NULL
    339    4     6     NULL       0(or NULL)     0       1
    我的意思是说,当f1,f2,f3都相等地时候f4,f5,f6,f7就合起来,
    就是说合并成101       5         6         1     1     1    1
      

  6.   

    to: aierong(皑婀瑢-数据库XML.NET联盟会局长) 
    你那样写没有这行啊,
    101    5     6     1       1             1      1变成
    101    5     6     1       1             0      0
    101    5     6     0       0             1      1
    了。:(
      

  7.   

    select * into t3 from (
    select F1,F2,F3,F4,F5,0 F6,0 F7,0 flag from T1
    union all
    select F1,F2,F3,0 F4,0 F5,F6,F7,1 flag from T2 and not exists (select 1 from T1 where f1=t2.f1 and f2=t2.f2 and f3=t2.f3)
    ) tem order by f1,flagselect * from t3
      

  8.   

    服务器: 消息 156,级别 15,状态 1,行 4
    在关键字 'and' 附近有语法错误。
    服务器: 消息 170,级别 15,状态 1,行 5
    第 5 行: ')' 附近有语法错误。
      

  9.   

    把and改成where可以执行,可是执行后消除了一行,就是
    变成101    5     6     1       1             0      0
    了,我要的是101    5     6     1       1       1    1
    也就是合并,不是消除啊。
    有没有办法,要是麻烦的话再建一个临时表T4从T3的结果里面取也行啊。
      

  10.   

    select F1,F2,F3,sum(f4) f4,sum(f5) f5,sum(f6) f6,sum(f7) f7 into t3 from (
    select F1,F2,F3,F4,F5,0 F6,0 F7,0 flag from T1
    union all
    select F1,F2,F3,0 F4,0 F5,F6,F7,1 flag from T2
    ) tem group by F1,F2,F3 order by f1,flagselect * from t3