我这里有两张数据表,结构完全一样,
table1和table2表。字段定义为:
Num int 4  (为主键)
sl varchar2
(我用的是sqlserver200)
第一个表的内容如下:
1001 100
1002  220
1003  213
第二个表的内容如下:
1001  110
1002  120
1003  310
如何实现建立第三个table3,而它的结构和table1和table2完全一样,内容如下:
1001 210
1002  340
1003  523
请各位高手帮忙,问题解决马上给分!!!

解决方案 »

  1.   

    对了,最好是能提供相关组件,我用过一个TBatchMove组件效果不是很好。不知是否还一样其它更合适一些的组件?
      

  2.   

    如果不需要建立一个真实的Table3,只是得到一个Table3的结果的话,写查询就可以了
      

  3.   

    建立三个数据库连接con1,con2,con3.con1读表1的数据,con2读表2的数据,con3建立与表3的连接.把con1和con2的记录相加存入con3中,con1,con2,con3数据库指针下移.直到结束.最后关闭3个数据库连接
      

  4.   

    create table table3
    (
      Num int 4  not null primay key,
      sl varchar(20) null
    )insert intto table3 
    select num as num,sum(sl) as sl from
    (
      select num,sl from table1
      union
      select num,sl from table2
    ) aaa
    group by num
      

  5.   

    Insert into Table3(Num,S1)
    select Num,Convert(bigInt,S1)+(select Convert(bigInt,S1) from Table2 where Table2.Num=Table1.num) from Table1//BigInt 或 Int 根据你的Varchar字段中数据的大小而定,若超出Int的则使用BigInt
      

  6.   

    select a.id,a.value+b.value into c where a.id=b.id
      

  7.   

    create table table3
    (
      Num int 4  not null primay key,
      sl varchar(20) null
    )insert intto table3 
    select num,cast(sl as varchar) from
    (
      select num as num,sum(sl) as sl from
      (
        select num,cast(sl as int) from table1
        union
        select num,cast(sl as int) from table2
      ) aaa
      group by num
    )bbb
      

  8.   

    jinjazz(我是jin) 
    select a.id,a.value+b.value into c where a.id=b.id正解
      

  9.   

    我试了试,基本可以,只是不太理想,现在两个表的合并只能实现数据的全部相加或相减,
    我是想实现部分数据相加还有部分数据相减,如table1和table2的1001和1003的sl数据相加,
    1002的sl数据相减。然后汇总到table3表.我这个问题的意思是想直接用delphi代码控制,我之前用过一个TBatchMove控件,只能实现数据的追加和更新,所以不合适,不知是否有类似的控件。不好意思,昨天一天停电,没能来得及看帖,报歉!!!!
      

  10.   

    你要每条记录都有特殊处理只能用query取出来
    while not qry.eof do begin
    ...
    qry.next
    end;
    语句逐条判断了如果判断条件少的话可以select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
    union  {--all}
    select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
    into c
    其中all表示不去掉重复记录
    当然c必须要先存在
      

  11.   

    select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
    union  {--all}
    select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
    into c
      

  12.   

    sorry----b4楼上insert into c
    select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
    union  {--all}
    select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
    其中all表示不去掉重复记录
    当然c必须要先存在
      

  13.   

    insert into t3 select t1.id,(t1.value+t2.value)value from t1,t2 where t1.id=t2.id;
    搞定收工..