有这样一个数据表,如图,消费金额是float,消费时间是datetime,我想用一条sql语句查询某用户本日消费总额,我自己想的语句是ADOquery2_xiaofei.SQL.Text :='select sum(消费金额) as 合计 from 消费记录表 where 卡片编号='''+idtemp+''' and 消费时间 like '''+DateToStr(Date)+'%'' ';可惜未能实现,请教各位高手应该怎么做。消费时间记录日期和时间还有,添加消费记录的语句为date_time:=formatdatetime('yyyy"-"m"-"d HH:NN:SS',now);adocommand1.CommandText:='insert into 消费记录表([消费记录编号],[卡片编号],[消费金额],[消费时间],[消费后金额],[创建时间],[消费地点编号],[公司编号],[部门编号],[员工编号]) values('''+inputnum+''','''+idtemp+''','+floattostr(input_money)+','''+date_time+''','+floattostr(yu_e)+','''+create_time+''','''+locus_ID+''','''+company_id+''','''+department_ID+''','''+worker_ID+''')';此语句没问题。 请问查询本日消费总额那条语句应该怎样修改呢?
热心网友
最稳妥的办法是:var wYear, wMonth, wDay: Word;begin DecodeDate(Date, wYear, wMonth, wDay); ADOquery2_xiaofei.SQL.Text :='select sum(消费金额) as 合计 from 消费记录表 where (卡片编号='''+idtemp+''') and (year(消费时间)='+IntToStr(wYear)+') and (month(消费时间)='+IntToStr(wMonth)+') and (day(消费时间)='+IntToStr(wDay)+')';
热心网友
改成这样:ADOquery2_xiaofei.SQL.Text :='select sum(消费金额) as 合计 from 消费记录表 ''where 卡片编号=''' + idtemp + ''' and 消费时间 = '''+DateToStr(Date)+ ''' ';应该用=,因为消费时间是日期类型的字段,应该用=作比较,LIKE一般只用于字符串类型字段的比较
热心网友
'select sum(消费金额) as 合计 from 消费记录表 where 卡片编号='''+idtemp+''' and 消费时间 like '''+DateToStr(Date)+'%'' ';要查询本日的总金额:select sum(消费金额) as 合计 from 消费记录表 where 卡片编号='''+idtemp+''' and 消费时间 = '''+cast(Date)+'%'' ';用=看看