update 表名 set 账户余额 = 收入总额 - 支出总额不是这个意思么?你的总额是怎么计算出来的
大概代码如下:这样只是执行了一行,如何才能让他循环执行,直到最后一行procedure TFrmAccountType.bsSkinButton6Click(Sender: TObject); var Income,Expense,Balance:Currency; s:string; begin s:='select 账户类别,sum(收入金额)as 收入总额 from Income where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别'; with Query2 do begin Close; SQL.clear; SQL.add(s); try open; except execsql; end; end; Income:=Query2.Fields[1].AsCurrency; with Table1 do begin edit; fieldValues['收入总额']:=Income; post; end; s:='select 账户类别,sum(支出金额)as 支出总额 from expense where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别'; with Query2 do begin Close; SQL.clear; SQL.add(s); try open; except execsql; end; end; Expense:=Query2.Fields[1].AsCurrency; with Table1 do begin edit; fieldValues['支出总额']:=Expense; post; end; Balance:=StrToCurr(IncomeSum.Text)-StrToCurr(ExpenseSum.Text); with Table1 do begin edit; fieldValues['账户余额']:=Balance; post; end; end;
加个计算字段也可以吧。而且 s:='select 账户类别,sum(收入金额)as 收入总额 from Income where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别'; s:='select 账户类别,sum(支出金额)as 支出总额 from expense where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别'; 这两个语句可以一个SQL中进行的,直接算出来账户余额了select a.账户类别,sum(a.收入金额) as 收入总额,sum(b.支出金额)as 支出总额, sum(a.收入金额)-sum(b.支出金额) as 账户余额 from Income a,expense b where a.账户类别=b.账户类别 group by a.账户类别 注意可能要用到外联接
update table set 账户余额=收入总额-支出总额
这样更新一下就行了。
var
Income,Expense,Balance:Currency;
s:string;
begin
s:='select 账户类别,sum(收入金额)as 收入总额 from Income where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别';
with Query2 do
begin
Close;
SQL.clear;
SQL.add(s);
try
open;
except
execsql;
end;
end;
Income:=Query2.Fields[1].AsCurrency;
with Table1 do
begin
edit;
fieldValues['收入总额']:=Income;
post;
end;
s:='select 账户类别,sum(支出金额)as 支出总额 from expense where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别';
with Query2 do
begin
Close;
SQL.clear;
SQL.add(s);
try
open;
except
execsql;
end;
end;
Expense:=Query2.Fields[1].AsCurrency;
with Table1 do
begin
edit;
fieldValues['支出总额']:=Expense;
post;
end;
Balance:=StrToCurr(IncomeSum.Text)-StrToCurr(ExpenseSum.Text);
with Table1 do
begin
edit;
fieldValues['账户余额']:=Balance;
post;
end;
end;
s:='select 账户类别,sum(收入金额)as 收入总额 from Income where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别';
s:='select 账户类别,sum(支出金额)as 支出总额 from expense where 账户类别='+QuotedStr(bsSkinDBEdit1.Text)+' group by 账户类别'; 这两个语句可以一个SQL中进行的,直接算出来账户余额了select a.账户类别,sum(a.收入金额) as 收入总额,sum(b.支出金额)as 支出总额,
sum(a.收入金额)-sum(b.支出金额) as 账户余额
from Income a,expense b
where a.账户类别=b.账户类别
group by a.账户类别
注意可能要用到外联接