成本费用存在4个表中,比如cb1,cb2,cb3,cb4,字段分别为fy1,fy2,fy3,fy4,我如何在delphi中利用sql语句统计成本费用总和,本人新手,希望能详细说下.这四个成本表没有任何关联,再说具体一点,比如说在form1中有一个edit框,现在要做的就是,根据edit框中输入的单号,在成本表里进行检索,检索出来后将他们的总和加起来就是总的成本了。不知我表达清楚了没。

解决方案 »

  1.   

    我这样写不知道对不对sql.clear;
              sql.add('select sum(a+b+c+d) as aNum from');
              sql.add('(select dl_tit as a from dl_fyxx where dl_id='+QuotedStr(edit12.text));
              sql.add('union all');
              sql.add('select other_tit as b from other_fyxx where other_id='+QuotedStr(edit12.text));
              sql.add('union all') ;
              sql.Add('select cd_tit as c from cd_fyxx where cd_id='+QuotedStr(edit12.Text));
              sql.Add('union all');
              sql.add('select hy_tit as d from hy_fyxx where hy_id='+QuotedStr(edit1.text)+')  as p ') ;
              dm.QCBFY.Open;
              cb:=dm.QCBFY.FieldByName('aNum').AsInteger;
      

  2.   

    这样会慢死select sum(a.a)+sum(b.b)+sum(c.c)+sum(d.d) as aNum from dl_fyxx a,other_fyxx b,cd_fyxx c,hy_fyxx d where a.dl_id = b.other_id and a.dl_id = c.cd_id and a.dl_id = d.hy_id
    and a.dl_id='+QuotedStr(edit12.text);以上代码没测试,不知道对不对,但是你不要搞几个SELECT ,不然很慢的
      

  3.   

    因为楼主的四个成本表没有任何关联的字段存在,所以只有加个表求和,再加起来哦。
    两种方法:
      1、写在一个查询语句中
      with adoquery1 do begin
        sql.clear;
              sql.add('select sum(a) as aNum from');
              sql.add('(select dl_tit as a from dl_fyxx where dl_id='+QuotedStr(edit12.text));
              sql.add('union all');
              sql.add('select other_tit  from other_fyxx where other_id='+QuotedStr(edit12.text));
              sql.add('union all') ;
              sql.Add('select cd_tit  from cd_fyxx where cd_id='+QuotedStr(edit12.Text));
              sql.Add('union all');
              sql.add('select hy_tit from hy_fyxx where hy_id='+QuotedStr(edit1.text)+')  as p ') ;
              dm.QCBFY.Open;end;