有這樣一個數據表,如圖,消費金額是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)+'%'' ';用=看看