select (select sum(free) from biao1)+(select sum(free) from biao2)
select office,sum(fee) from (select * from biao1 union select * from biao2) as biao group by office
如果不分组求sum ,写 select sum(fee) from (select * from biao1 union select * from biao2) as biao
同意笑天的写法 我还建议一下:select (select sum(free) from biao1)+(select sum(free) from biao2) as t1; 这样的话可以在DELPHI中更好的识别
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;
To 笑天,还有一点想问,就是如果第二个表如果没有数据,得出的结果将是NULL, to musicdog(白狮子),如果有查询条件是把所有office为P0001的Fee总和呢,该如何去写,多谢大家指点!分数晚上在给!
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
如果想防止空值,可以使用IsNull函数 select Isnull((select sum(free) from biao1)+(select sum(free) from biao2), 0) as TotalFree;
select sum(fee) from (select * from biao1 union select * from biao2) as biao
我还建议一下:select (select sum(free) from biao1)+(select sum(free) from biao2) as t1;
这样的话可以在DELPHI中更好的识别
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;
to musicdog(白狮子),如果有查询条件是把所有office为P0001的Fee总和呢,该如何去写,多谢大家指点!分数晚上在给!
select Isnull((select sum(free) from biao1)+(select sum(free) from biao2), 0) as TotalFree;
----
学习,挺好的