老大们,帮帮忙!!sql问题,50分,解决就给!

chenpppp5678 2002-08-19 08:23:16
我想从5张表里提数据,每张表字段相同,其中有一字段为开户日期kaihui_date,类型string,现在我要写一条sql语句,根据两个datetimepicker设立的时间段,将五张表中属于这段时间的记录提出来,怎样写?
with query2 do begin
close;
with sql do begin
clear;
if (checkbox1.state=cbchecked)or (checkbox2.state=cbchecked) then
begin
add('select * from jiben_account,yiban_account,temp_account,zuanyong_account,qita_account');
//add('select * from yiban_account');
//add('select * from temp_account');
// add('select * from zuanyong_account');
// add('select * from qita_account');
// add('number<>''''');
add('where strtodate(kaihui_date)<=t2.date and strtodate(kaihui_date)>=t1.date') ;
end;
end;

open;
query2.first;
为什么会报错??
...全文
31 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenpppp5678 2002-08-20
  • 打赏
  • 举报
回复
根本编译不过!
xuyunbo 2002-08-20
  • 打赏
  • 举报
回复
'where kaihui_date<='"'+datetostr(t2.date)+'"' and kaihui_date>='"'+datetostr(t1.date)+'"') ;
这样才可以的!
tigerchina 2002-08-20
  • 打赏
  • 举报
回复
不好意思,Oracle中是to_date
select * from jiben_account
where to_date(kaihui_date) between :t1 and :t2
union
select * from yiban_account
where to_date(kaihui_date) between :t1 and :t2
union
select * from temp_account
where to_date(kaihui_date) between :t1 and :t2
union
select * from zuanyong_account
where to_date(kaihui_date) between :t1 and :t2
union
select * from qita_account
where to_date(kaihui_date) between :t1 and :t2

Query.ParamByName('t1').AsDate := T1.Date;
Query.ParamByName('t2').AsDate := T2.Date;
Query.Open;

另外,你的kaihui_date是什么格式的,是否跟你数据库中的日期格式相同,不同的话要改,如To_Date(kaihui_date,'yy.mm.dd')
tigerchina 2002-08-20
  • 打赏
  • 举报
回复
你原来kaihui_date的格式是什么?现在报什么错?
chenpppp5678 2002-08-20
  • 打赏
  • 举报
回复
还是不行,难道一定要将kaihui_date的字段定为date型码,我原来是string
luoshumeng 2002-08-20
  • 打赏
  • 举报
回复
字符串比较两个日期是比较不出正确结果的!
luoshumeng 2002-08-20
  • 打赏
  • 举报
回复
将string改为日期的不就OK了,你不要以为string好写,当你要查询的时候就麻烦了!改成Datetime的类型的就OK了!
luoshumeng 2002-08-20
  • 打赏
  • 举报
回复
你的库设计不对,语句写得也是错误的!
chenpppp5678 2002-08-19
  • 打赏
  • 举报
回复
又报错
type mismatch in expression
fwwxxjingling 2002-08-19
  • 打赏
  • 举报
回复
to chenpppp5678(chen) :
**************************************************************
注意:
Sql 语句并不认识你写的t2.date 和t1.date是变量,他认为那是数据库中的字段,他将t2认为表名,将data认为是t2表的字段,所以报错!!
你应该这样写:
'where kaihui_date<='''+datetostr(t2.date)+''' and kaihui_date>='''+datetostr(t1.date)+'''') ;

**************************************************************
好啦,我不多说啦!!
去试吧!!
oracle_lover 2002-08-19
  • 打赏
  • 举报
回复
当然不会
chenpppp5678 2002-08-19
  • 打赏
  • 举报
回复
我如果把日期型转化位字符型比较,是否会得到正确结果???
oracle_lover 2002-08-19
  • 打赏
  • 举报
回复
sql语句不应该出现Delphi的关键字,如strtodate(), t1.date, t2.date数据库不会认识的;to_date('format',string)是数据库的函数,将字符串转为日期。如果是Paradox数据库,format应该只能是MM/dd/yyyy。''两个引号是为了在一对单引号括起来的字符串中显示单引号。上面方法能行吗?希望能帮到你。
SayForever 2002-08-19
  • 打赏
  • 举报
回复
這是ORACLE的語法.
chenpppp5678 2002-08-19
  • 打赏
  • 举报
回复
这是什么意思?说明一下??
oracle_lover 2002-08-19
  • 打赏
  • 举报
回复
最后一招,如果不行,我也没办法了
add('select * from jiben_account,yiban_account,temp_account,zuanyong_account,qita_account');

add(' where to_date(''yyyy-MM-dd'','+DateToStr(t1.date)+')<='+'to_date(''yyyy-MM-dd'','+kaihui_date+') and '+'to_date(''yyyy-MM-dd'','+DateToStr(t2.date)+')>='+'to_date(''yyyy-MM-dd'','+kaihui_date+')') ;
chenpppp5678 2002-08-19
  • 打赏
  • 举报
回复
这里没问题,只是sql语句不对,到底怎么办????????????????????
SayForever 2002-08-19
  • 打赏
  • 举报
回复
if (checkbox1.state=cbchecked)or (checkbox2.state=cbchecked) then改為:
if (checkbox1.bchecked=True)or (checkbox2.bchecked=true) then
試一下.
chenpppp5678 2002-08-19
  • 打赏
  • 举报
回复
不对,我是过了,而且我也没有DATE字段
oracle_lover 2002-08-19
  • 打赏
  • 举报
回复
你有没有用参数呀?不会有问题呀。

另外,你的表里有没有字段名为date的,改掉,这是数据库的保留字。
加载更多回复(6)

5,931

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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