tablename:cjdata
id temperature
1 12.39
2 15.36
3 14.25
1 15.37
2 13.78
3 15.82
temperature的类型是char型
要分别求各ID号temperature的平均值 显示到edit中
代码如下:
procedure TForm1.BitBtn1Click(Sender: TObject);
var sqlstr:string;
tempavg:real;
begin
sqlstr:='select avg(cast(temperature as numeric(8,2))) as tempavg from cjdata where id=1;
with adoquery1 do
begin
sql.Clear;
sql.Add(sqlstr);
prepared;
open;
//tempavg:=ExecSQL;
end;
edit1.Text:=floattostr(tempavg);
end end.
在sql的查询分析器中执行成功,并显示出了平均值,而这里edit1中显示0 ,将open注释掉,换成tempavg:=ExecSQL;显示-1。
大侠们帮帮偶吧,急呀。
id temperature
1 12.39
2 15.36
3 14.25
1 15.37
2 13.78
3 15.82
temperature的类型是char型
要分别求各ID号temperature的平均值 显示到edit中
代码如下:
procedure TForm1.BitBtn1Click(Sender: TObject);
var sqlstr:string;
tempavg:real;
begin
sqlstr:='select avg(cast(temperature as numeric(8,2))) as tempavg from cjdata where id=1;
with adoquery1 do
begin
sql.Clear;
sql.Add(sqlstr);
prepared;
open;
//tempavg:=ExecSQL;
end;
edit1.Text:=floattostr(tempavg);
end end.
在sql的查询分析器中执行成功,并显示出了平均值,而这里edit1中显示0 ,将open注释掉,换成tempavg:=ExecSQL;显示-1。
大侠们帮帮偶吧,急呀。
sqlstr:='select avg(cast(temperature as numeric(8,2))) as tempavg from cjdata where id=1';
with adoquery1 do
begin
sql.Clear;
sql.Add(sqlstr);
prepared;
open;
edit1.Text:=floattostr(Fields[0].Value);
end;
三楼的是完全正确的。