有如下两个表,biao1,和biao2,表一的字段为:office,和free
表二的字段也是office 和free,请问如何用sql语句一下子求出sum(fee)。也就是两个表中所有的free的和
在线等待

解决方案 »

  1.   

    select (select sum(free) from biao1)+(select sum(free) from biao2)
      

  2.   

    select office,sum(fee) from (select * from biao1 union select * from biao2) as biao group by office
      

  3.   

    如果不分组求sum ,写
    select sum(fee) from (select * from biao1 union select * from biao2) as biao
      

  4.   

    同意笑天的写法
    我还建议一下:select (select sum(free) from biao1)+(select sum(free) from biao2) as t1;
    这样的话可以在DELPHI中更好的识别
      

  5.   

    if ADODataSet1.Active then
      ADODataSet1.Active:=False;
    ADODataSet1.CommandText:='select (select sum(free) from biao1)+(select sum(free) from biao2) as TotalFree';
    ADODataSet1.Active:=True;
    Edit1.Text:=ADODataSet1.Fields[0].AsString;
      

  6.   

    To 笑天,还有一点想问,就是如果第二个表如果没有数据,得出的结果将是NULL,
    to musicdog(白狮子),如果有查询条件是把所有office为P0001的Fee总和呢,该如何去写,多谢大家指点!分数晚上在给!
      

  7.   

    select office,sum(fee) from (select * from biao1 union select * from biao2) as biao where office='P0001' group by office或者select office,sum(fee) from (select * from biao1 where office='P0001' union select * from biao2 where office='P0001') as biao group by office
      

  8.   

    如果想防止空值,可以使用IsNull函数
    select Isnull((select sum(free) from biao1)+(select sum(free) from biao2), 0) as TotalFree;
      

  9.   

    回复人: TOMWLD(笑天)  
    ----
    学习,挺好的