关于select count(*) 赋值问题

nihm023 2008-06-03 09:07:30

mstrSQL:='select count(*) as cou '+
'from Deal ' +
'where '+ quotedstr(mstr1)+'='+quotedstr(mstr2)+
'and '+
'dealdate>='+quotedstr(Da)+'and dealdate<='+ quotedstr(das);
with ADOQuery2 do
begin
close;
with SQL do
begin
Clear;
Add(mstrSQL);
end;
Open;
if Eof and Bof then
begin
end
else
begin //开始处理
setlength(jilu,k+1);
jilu[k]:=strtoint(Trim(FieldByName('cou').AsString)); //储存该商品项的频数
end;
end;
当count(*)值为空时,红色部分是不能进行赋值的
...全文
258 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzmhero 2008-06-04
  • 打赏
  • 举报
回复
问题是在

'where '+ quotedstr(mstr1)+'='+quotedstr(mstr2)+ 这里
---------》
'where '+ mstr1+'='+quotedstr(mstr2)+
Heyongfeng 2008-06-04
  • 打赏
  • 举报
回复
SQL语句中count(*) 返回值不可能为空,是最小为0的整数,有且只有一行值.

使用FieldByName('cou').AsInteger访问.
auqfiudh 2008-06-04
  • 打赏
  • 举报
回复
UP 2
zlb_chen 2008-06-04
  • 打赏
  • 举报
回复
同解
阿三 2008-06-03
  • 打赏
  • 举报
回复
同意2楼的,select count(*)要用值是否大于零来判断而不能看是否为eof。
nihm023 2008-06-03
  • 打赏
  • 举报
回复
谢谢,我试下
kaikai_kk 2008-06-03
  • 打赏
  • 举报
回复
with  ADOQuery2 do
begin
close;
sql.Clear;
sql.Text:=mstrSQL;
Open;
{ if Fields[0].AsInteger<>0 then}
{ begin}
setlength(jilu,k+1);
jilu[k]:=Fields[0].AsInteger;
{ end;}
end;


select count(*)... 语句,结果返回的符合条件的总数量,没有符合的记录就返回0
所以判断是不是0就行了,不须要Eof或Bof,结果集也就只有一行
nihm023 2008-06-03
  • 打赏
  • 举报
回复
我自己解决的办法很笨,先是按mstrSQL:='select * '+
'from Deal ' +
'where '+ quotedstr(mstr1)+'='+quotedstr(mstr2)+
'and '+
'dealdate>='+quotedstr(Da)+'and dealdate <='+ quotedstr(das); 扫描表,如果满足if Eof and Bof then 不操作jilu[k]:=strtoint(Trim(FieldByName('cou').AsString)); 否则的话再写以上那段代码。
请问有更好的办法解决吗?高手请指点下啊

2,507

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧