刚解决完Adoquery函数问题后有新问题了:重复一下刚才的问题
我用的是Delphi+Access我要统计一个表1内的字段(消费金额),但要以姓名作为条件.
比如:在Edit1中输入姓名为"甲"的客户,ButtonClick事件后便要统计"甲"消费的金额总数.显示在Label中代码:
      datasource2.DataSet:=adoquery2;
      begin
       adoquery2.Close;
       adoquery2.SQL.Clear;
       ADOQuery2.SQL.Add('select 姓名,SUM(消费金额) from m_buy Group by 姓名 where 姓名 like '''+'%'+edit1.text+'%'+'''');
       adoquery2.Open;
       adoquery2.Active:=true;
     end;
      Label4.Caption:=inttostr(ADOquery2.fields[2].value);出错提示:语法错误(操作符丢失)在查询表达式‘姓名 where 姓名 like ''%'甲'%''’中

解决方案 »

  1.   

    ADOQuery2.SQL.Add('select 姓名,SUM(消费金额) from m_buy Group by 姓名 where 姓名 like '''+'%'+edit1.text+'%'+'''');语句中,GROUP BY 应该放在WHERE语句的后面,因此应该改成:
    ADOQuery2.SQL.Add('select 姓名,SUM(消费金额) from m_buy where 姓名 like '''+'%'+edit1.text+'%'+''' Group by 姓名 ');
      

  2.   

    运行后出现了错误:Project Project.exe raised exception class ElistError with message'list index out of bounds(2)'.Process stopped.use setp or run to continue.主要是不明白什么是‘list index out of bounds(2)’
      

  3.   

    按你的这个Select查询, 结果数据集中只有两个字段, 分别是ADOquery2.fields[0]和ADOquery2.fields[1], 对并不存在的ADOquery2.fields[2]进行引用的话, 就会出现错误了: list index out of bounds(2)