select * from yourtable where name='' and age bewteen..and.. and sex=''

解决方案 »

  1.   


    SqlStr := 'select * from yourtable';
    SqlStr := SqlStr + ' where name= '''+ EditName.Text+ ''' and age  '+ ComboBoxage1.Text  + Editage.text  + ' and sex = ''' + Combobox.Text + ''''
      

  2.   

    可以用动态SQL语句就行了,因为delphi执行SQL是作为一个字符串提交到后台的。
    但是注意edit中的值不要忘记了用引号引起来。
    '...name=' + '"' + Trim(Edit1.Text) + '"' + .....
      

  3.   

    我现在的电脑上没有装Delphi,但可以给你一个思路。
    你可以Combobox中每改变一次,你就将Sql语句的条件变换一次,在radiogroup中,每点击一个radio,就改变一次条件,这样做,比你在最后做判断要省事。
    然后Sql语句就可以写:
    SqlStr := 'select * form TABLENAME where'
    SqlStr := SqlStr + 条件1 + 'and' + 条件2 + 'and' + 条件3
    就可以了。
      

  4.   

    Funciton CreateSql(QueryTableName,LogicSQL:String):String
    Var
      SqlCondition:String;
      FieldData:String;
    Begin
      for i := 0 to componentcount -1 do
      Begin
        if Compoents[i] is TCustomEditthen
          FieldData := TCustomEdit(Components[I]).Text
        else if Components[I] is TComboBox then
          FieldData := TComboBox(Components[I]).Text;
        
        SqlCondition := SqlCondition + LogicSQL + ' '+ components[I].name + '='''+ FieldData;
      end;
      delete(SqlCondition,pos(LogicSQL,SqlCondition),length(LogicSQL));
      Result := 'select * from ' + QueryTableName + ' where '+ SqlCondition;
    end;
      

  5.   

    unit mainform;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, DBTables, Grids, DBGrids, ExtCtrls;type
      Tfrm_main = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        Edit3: TEdit;
        RadioGroup1: TRadioGroup;
        Button1: TButton;
        Button2: TButton;
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        Query1: TQuery;
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private    { Private declarations }
      public
        { Public declarations }
      end;var
      frm_main: Tfrm_main;implementation{$R *.dfm}procedure Tfrm_main.FormCreate(Sender: TObject);
    begin
    query1.Close;
    query1.SQL.Clear;
    query1.SQL.Add('select * from "student.db"');
    query1.Prepare;
    query1.ExecSQL;
    query1.Open;
    end;procedure Tfrm_main.Button1Click(Sender: TObject);
    var
    i:integer;
    str1,str2,log:string;
    arr:array[1..4]of string;
      function gstt(curr:tedit):boolean;
       begin
         result:=false;
         if curr.Text<>'' then
         result:=true;
         end;
    begin
    if radiogroup1.ItemIndex=0 then
    log:='or'
    else
    log:='and';
      if gstt(edit1) then
        arr[1]:='(name='''+edit1.Text+''')';
      if gstt(edit2) then
        arr[2]:='(phone='''+edit2.Text+''')';
      if gstt(edit3) then
        arr[3]:='(age='''+edit3.Text+''')';
      arr[4]:='';
      for i:=1 to 4 do
      begin
      arr[4]:=arr[4]+arr[i];
      if arr[i]<>'' then
      begin
        str2:=str2+log+arr[i];
        end;
      if arr[4]='' then
      begin
      showmessage('kjdkjfkjsdkfjksdjk');
      exit;
      end;
      str1:='select * from "student.db" where ';
      str1:=str1+''+str2;
      with query1 do
      begin
      close;
      sql.Clear;
      sql.Add(str1);
      prepare;
      execsql;
      open;
      end;  end;end;end.