请求快速解答SQl查询的问题。。。急。(30分)

  • 主题发起人 主题发起人 狼牙
  • 开始时间 开始时间

狼牙

Unregistered / Unconfirmed
GUEST, unregistred user!
sql7中一个日期字段,我要查询一段到一段的数据,该如何写阿?
比如:
select * from customer where join_date>01-01-01 and join_date<01-06-01
order by join_date

急救!!!
怎么写阿???
 
你写的正确
 
select * from customer where join_date>'01-01-01' and join_date<'01-06-01'
order by join_date

注意打引号!
 
问题是。。。。。执行后数据dbgrid里面显示为空阿。。。why?
确定有在该数据范围内的数据!!!
但为何不行?
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
begin
ADOQuery1.SQL.Clear;
// ADOQuery1.SQL.Add(Edit1.Text);
str:='select join_date from results ';
str:=str+'where join_date>:dtime';
ADOQuery1.SQL.Add(str);
// adoquery1.SQL.Add('select join_date from results where join_date>:dtime');
ADOQuery1.ExecSQL;
ADOQuery1.Active:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin


adoquery1.Parameters.ParamByName('dtime').Value:=edit2.text;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
showmessage(adoquery1.Parameters.ParamByName('dtime').Value);
end;

end.
执行后没有任何数据显示,
但在属性窗口里面设定同样的sql语句,却能正常显示?
为什么???啊阿 ,阿。阿阿我 要死了扼。
 
to 狼牙
select * from customer where join_date>01-01-01 and join_date<01-06-01
order by join_date
执行的时候没有出错吗? 大家有没有想到在执行SQL语句的时候,对于日期型
的字段他的表示要符合SQL的规则,01-01-01 应该为 01/01/01这样表示
你逝世看,行不行
大家有没有更好的建议一起讨论讨论
 
select * from customer where join_date between '01-01-01' and '01-06-01'
order by join_date
 
试下这个吧,祝你好运!
select * from customer where join_date between #2001-01-01# and #2001-06-01#
order by join_date

 
to strock and xieyj
你们那样的表达式中那两个日期应该改为 ‘2001/01/01’这样的吧?我在Sql Explorer 4.0
中试了一下'01-01-01' 通不过,不知道是不是这样的? 你们的那两句有测试过吗?
 
唉,我也遇到过

这样解决的
.用参数
或者颠倒条件的比较顺序,用">",具体我忘了,你试一下就可以了,比较怪
 
我在C++Builder中同样碰到了这个问题,前面几位大虾说的我都试过,好像不行,我怀疑是数据类型转换的问题,Delphi中我没试过,过几天试一下!
 
可能是你保存日期型字段与我们存储的不符,我这里调试过都可以的
 
sql:='SELECT * FROM tablename WHERE time_in>=#'+formatdatetime('yyyy/mm/dd',sdt - 1)+'# '
+' AND time_out<=#'+formatdatetime('yyyy/mm/dd',edt + 1)+'# ';
 
query1.close;
query1.sql.clear;
query1.sql.add('select * from customer');
query1.sql.add('where join_date>;data1');
query1.sql.add('where join_date<;data2');
query1.ParamByName('data1').asdatatime:=你的第一个时间
query1.ParamByName('data2').asdatatime:=你的第二个时间
query1.open;


 
我同意white0212的意见。用query控件如果要得到查询结果时,一定用open不能用execsql
 
查询时应为这样:
select * from customer where join_date>#01/01/01# and join_date<#01/06/01#
order by join_date
 
with adoquery1 do
with SQL do
clear;
add('select * from kkk where time<=:t1 and time>=:t1');
end;
open;
end;
parameters[0]:=strtodatetime(datetimetostr('2001-8-9')+' 0:00:00');
parameters[1]:=strtodatetime(datetimetostr('2001-8-9')+'23:59:59');


好像是这个样子的
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls;

type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ADOQuery1.Open;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from clientinfo');
ADOQuery1.SQL.Add('where buydate>:date1');
ADOQuery1.SQL.Add('and buydate<:date2');
ADOQuery1.Parameters.ParamByName('date1').Value:=strtodate(edit1.Text);
ADOQuery1.Parameters.ParamByName('date2').Value:=strtodate(edit2.Text);
ADOQuery1.Open;
end;

end.

谢谢各位。已通过。
 
后退
顶部