怎样写这个sql,已困扰我两天了,请帮忙(105分)

  • 主题发起人 主题发起人 英国病人
  • 开始时间 开始时间

英国病人

Unregistered / Unconfirmed
GUEST, unregistred user!
d4,dbase库
my.dbf表有字段
RIQI(日期型)、SHIJIAN(字符型)、MYDATA(字符型),
-------------------------------------------------
2000-1-1 9:00:00 asdfadsfa

我想用一个query的sql查询 2000-1-1 8:30:00 到 2000-1-15 8:30:00
之间的所有数据,我该怎样写这个SQL,我的程序如下。

a1:="2000-1-1 8:30:00";
a2:="2000-1-15 8:30:00";
DataModule1.QueryTmp.SQL.Add('Select * From my.dbf');
DataModule1.QueryTmp.SQL.Add(' Where RIQI+
TO_DATE(SHIJIAN,"HH:MM:SS") BETWEEN "'+a1+'" AND "'+a2+'"');
我知道我写的SQL很可笑,但我的意思应该表达清楚了,我很着急,请您帮助。
注:只用dbase,不用其它数据库。
 
1)dbase中时间常量的分隔符是"吗?
2)dbase中TO_DATE(SHIJIAN,"HH:MM:SS")返回的是否是 yyyy-mm-dd hh:mm:ss?
dbase不熟。
 
把日期作為日期型參數傳入,把'8:30:00'拆開傳入
select * from my.dbf
where riqi between date1 and date2
and substring(SHIJIAN,1,1) between '8' and '8'
and substring(SHIJIAN,3,2) between '30' and '30'
 
以下代碼試試:(本人已債在D5下測試)
procedure TForm1.Button1Click(Sender: TObject);
VAR AT1,AT2:TDATETIME;
a1,a2,t1:string;
BEGIN
a1:='2000/1/1';
a2:='2000/1/15';
t1:='8:30:00';
AT1:=STRTODATE(A1);
AT2:=STRTODATE(A2);
query1.Close;
query1.sql.clear;
Query1.SQL.Add('Select *');
Query1.SQL.Add('FROM test.dbf');
Query1.SQL.Add('Where (RIQI=:Ata)');
Query1.SQL.Add('AND (SHIJIAN>=:tb)');
Query1.parambyname('ata').asdatetime:=at1;
Query1.parambyname('tb').asstring:=t1;
Query1.SQL.Add('union');
Query1.SQL.Add('Select *');
Query1.SQL.Add('FROM test.dbf');
Query1.SQL.Add('where (RIQI=:Atb)');
Query1.SQL.Add('and (SHIJIAN<=:Tb)');
Query1.parambyname('tb').asstring:=t1;
Query1.parambyname('atb').asdatetime:=at2;
Query1.SQL.Add('union');
Query1.SQL.Add('SELECT RIQI, SHIJIAN, GH');
Query1.SQL.Add('FROM "test.dbf" Test');
Query1.SQL.Add('Where (RIQI>=:ata)AND(RIQI<=:atb)');
Query1.parambyname('ata').asdatetime:=at1+1;
Query1.parambyname('atb').asdatetime:=at2-1;
Query1.prepare;
Query1.open;
END;
請自己精簡.
 
調試環境d5+dbase數據庫
 
to g622:
TO_DATE,可能Local Sql不支持此函数,我想知道Local Sql支持的时间转换函数是什么?

To Jams:
:~(

To kwang:
好象不会查到是么东西吧?
 
sorry:change program:

procedure TForm1.Button1Click(Sender: TObject);
VAR AT1,AT2:TDATETIME;
a1,a2,t1:string;
BEGIN
a1:='2000/1/1';
a2:='2000/1/15';
t1:='8:30:00';
AT1:=STRTODATE(A1);
AT2:=STRTODATE(A2);
query1.Close;
query1.sql.clear;
Query1.SQL.Add('Select *');
Query1.SQL.Add('FROM test.dbf');
Query1.SQL.Add('Where (RIQI=:Ata)');
Query1.SQL.Add('AND (SHIJIAN>=:tb)');
Query1.parambyname('ata').asdatetime:=at1;
Query1.parambyname('tb').asstring:=t1;
Query1.SQL.Add('union');
Query1.SQL.Add('Select *');
Query1.SQL.Add('FROM test.dbf');
Query1.SQL.Add('where (RIQI=:Atb)');
Query1.SQL.Add('and (SHIJIAN<=:Tb)');
Query1.parambyname('tb').asstring:=t1;
Query1.parambyname('atb').asdatetime:=at2;
Query1.SQL.Add('union');
Query1.SQL.Add('SELECT RIQI, SHIJIAN, GH');
Query1.SQL.Add('FROM "test.dbf" Test');
Query1.SQL.Add('Where (RIQI>=:ata)AND(RIQI<=:atb)');
Query1.parambyname('ata').asdatetime:=at1;
Query1.parambyname('atb').asdatetime:=at2;
Query1.prepare;
Query1.open;
END;
找sql的書看union命令.
 
To 懒虫:
好长呀,我回去试试再回来。先谢谢你


To All:
有没有一行解决的呀?
 
dBase数据库的日期格式应该是mm/dd/yyyy吧。
试试下列语句:
select * from my.dbf where
(rq>"01/01/2000" or rq="01/01/2000" and sj>' 8:30:00')
and (rq<"01/15/2000" or rq="01/15/2000" and sj<' 8:30:00')
sj字段应该规范化为HH:MM:SS,小时数不足两位的在前面加' '。
 
too sorry: change program:(出錯再次更改)

procedure TForm1.Button1Click(Sender: TObject);
VAR AT1,AT2:TDATETIME;
a1,a2,t1:string;
BEGIN
a1:='2000/1/1';
a2:='2000/1/15';
t1:='8:30:00';
AT1:=STRTODATE(A1);
AT2:=STRTODATE(A2);
query1.Close;
query1.sql.clear;
Query1.SQL.Add('Select *');
Query1.SQL.Add('FROM test.dbf');
Query1.SQL.Add('Where (RIQI=:Ata)');
Query1.SQL.Add('AND (SHIJIAN>=:tb)');
Query1.parambyname('ata').asdatetime:=at1;
Query1.parambyname('tb').asstring:=t1;
Query1.SQL.Add('union');
Query1.SQL.Add('Select *');
Query1.SQL.Add('FROM test.dbf');
Query1.SQL.Add('where (RIQI=:Atb)');
Query1.SQL.Add('and (SHIJIAN<=:Tb)');
Query1.parambyname('tb').asstring:=t1;
Query1.parambyname('atb').asdatetime:=at2;
Query1.SQL.Add('union');
Query1.SQL.Add('SELECT RIQI, SHIJIAN, GH');
Query1.SQL.Add('FROM "test.dbf" Test');
Query1.SQL.Add('Where (RIQI>:ata)AND(RIQI<:atb)');
Query1.parambyname('ata').asdatetime:=at1;
Query1.parambyname('atb').asdatetime:=at2;
Query1.prepare;
Query1.open;
END;
找sql的書看union命令.
頭昏,sleep...
 
To Liuly:
好聪明的想法,我回去一并试试,明晚再来。
 
多人接受答案了。
 
后退
顶部