用SQL计算的求和如何用EDIT显示(20分)

  • 主题发起人 alanzhangxin
  • 开始时间
A

alanzhangxin

Unregistered / Unconfirmed
GUEST, unregistred user!
用SQL计算的求和如何用EDIT显示
如:select sum(leftcount) from goldcard where leftcount>0 and selled='是'
 
Edit.Text := Query.Fields[0].AsString
 
edit1.text:=query1.fieldbyname('ddd').asstring;
 
delphi中对于计算结果默认的column name 为column1......columnX
edit1.text: = query.fieldbyname('column1').asstring;
 
计算后的结果如何赋值?
用的ADOQUERY
 
adoquery1.Active:=false;
adoquery1.SQL.Clear;
adoquery1.SQL.add('select sum(leftcount) from goldcard where leftcount<>0 and selled=''是''');
adoquery1.open;
edit1.Text:=adoquery1.fieldbyname('column1').AsString;
提示找不到field 'column1'not found
 
adoquery1.Active:=false;
adoquery1.SQL.Clear;
adoquery1.SQL.add('select sum(leftcount) [red] column1 [/red] from goldcard where leftcount<>0 and selled=''是''');
adoquery1.open;
edit1.Text:=adoquery1.fieldbyname('column1').AsString

 
adoquery1.Active:=false;
adoquery1.SQL.Clear;
adoquery1.SQL.add('select sum(leftcount) as sumnum from goldcard where leftcount<>0 and selled=''是''');
adoquery1.open;
edit1.Text:=adoquery1.fieldbyname('sumnum').AsString;
 
adoquery1.Active:=false;
adoquery1.SQL.Clear;
adoquery1.SQL.add('select sum(leftcount) from goldcard where leftcount<>0 and selled=''是''');
adoquery1.open;
edit1.Text:=adoquery1.Fields[0].AsString;
最安全的做法是
adoquery1.Active:=false;
adoquery1.SQL.Clear;
adoquery1.SQL.add('select sum(leftcount) as aaa from goldcard where leftcount<>0 and selled=''是''');
adoquery1.open;
edit1.Text:=adoquery1.fieldbyname('aaa').AsString;
 
用edit1.Text := adoquery1.Fields[0].AsString;
或把sql语句中计算出来的列加上一个别名
select sum(leftcount) column1 from goldcard where leftcount>0 and selled='是'
就可以用edit1.Text := adoquery1.FieldByName('column1').AsString;
 
我发现墨认计算字段名为expr1000,
adoquery1.Active:=false;
adoquery1.SQL.Clear;
adoquery1.SQL.add('select sum(leftcount) from goldcard where leftcount<>0 and selled=''是''');
adoquery1.open;
edit1.Text:=adoquery1.fieldbyname('expr1000').AsString;
通过!
但dxdbgrid的内容清空,有何方法可不影响?
 
用另外一个Query做
 
多人接受答案了。
 
顶部