insert into table3 select top 1 a.*,b.class2 fromt table1 a,table 2 where a.id=b.id and a.datetime=b.datetimeinsert into table3 
select t.*
(
select select a.*,b.class2 fromt table1 a,table 2 where a.id=b.id and a.datetime<>b.datetime
union
select select a.id,b.datetime,a.class1,b.class2 fromt table1 a,table 2 where a.id=b.id and a.datetime<>b.datetime
)tinsert into table3 
select t.*
(
select select a.*,b.class2 fromt table1 a,table 2 where a.id<>b.id and a.datetime<>b.datetime
union
select select a.id,b.datetime,a.class1,b.class2 fromt table1 a,table 2 where a.id<>b.id and a.datetime<>b.datetime
)t

解决方案 »

  1.   

    insert into table3 (id,datetime,class1,class2)
       select a.id,a.datetime,a.class1,b.class2 from table1 a,table2 b where a.id=b.id and a.datetime=b.datetimeinset into table3 (id,datetime,class1,class2)
         select a.id,a.datetime,a.class1,b.class2 from table1 a full join table2 b on a.id=b.id where a.datetime is null or b.datetime is nullinset into table3 (id,datetime,class1,class2)
    select id,datetime,class1,'' from table1 where id not in (select id from table2)
    union all
    select id,datetime,'',class2 from table2 where id not in (select id from table1)
      

  2.   

    当 table1和table2 id 不相等时 
    插入到table3 两条纪录?
    此条件好象有问题?
      

  3.   

    没有在查询分析器里试过:P 仅供参考
    if exist(table1.id=table2.id and table1.datetime=table2.datetime)
    insert into table3 select table1.id,table1.datetime,table1.class1,table2.class2 from table1 full outer join table2 on table1.id=table2.idif exist(table1.id=table2.id and table1.datetime<>table2.datetime)这个可能不对。。
    insert into table3 select table1.id,table1.datetime,'',table2.class2 from table1 full outer join table2 on table1.id=table2.id
    insert into table3 select table1.id,table1.datetime,table1.class1,'' from table1 full outer join table2 on table1.id=table2.idif exist(table1.id<>table2.id)
    insert into table3 select table1.id,table1.datetime,table1.class1,'' from table1 
    insert into table3 select table2.id,table2.datetime,table2.class2,'' from table2
      

  4.   

    insert into table3 (id,datetime,class1,class2)
       select a.id,a.datetime,a.class1,b.class2 from table1 a,table2 b where a.id=b.id and a.datetime=b.datetime
    union all
         select a.id,a.datetime,a.class1,b.class2 from table1 a full join table2 b on a.id=b.id where a.datetime is null or b.datetime is null
    union all
    select id,datetime,class1,'' from table1 where id not in (select id from table2)
    union all
    select id,datetime,'',class2 from table2 where id not in (select id from table1)