edt1,edt2,edt3,edt4分别用于输入查询字段a,b,c,d,如果edt1,edt2,edt3,edt4全部输入值那么a,b,c,d查询出既满足edt1又满足edt2又满足edt3又满足edt4,如果只输入一个或2个值,那么查询它所相对应的字段满足所输入的条件

解决方案 »

  1.   


    var
      SQLStr : String;
    begin
      if edt1.Text <> '' then
        SQLStr := SQLStr + a条件
      if edt2.Text <> '' then
        SQLStr := SQLStr + b条件
      if edt3.Text <> '' then
        SQLStr := SQLStr + c条件
      if edt4.Text <> '' then
        SQLStr := SQLStr + d条件
    end
      

  2.   

    var
      sqlstr: string;
    begin
      sqlstr := format(' select * from tablename where f1 = isnull(''%s'',f1) '+
                             and f2 = isnull(''%s'',f2) 
                             and f3 = isnull(''%s'',f3)
                             and f4 = isnull(''%s'',f4)  ',[edit1.text,edit2.text,edit3.text,edit4.text]);
    end;
      

  3.   

    但是如果都不为空,就要同时满足abcd四个条件的查询,如果一个为空就要同时满足另外3个条件,如果2个为空就要同时满足另外2个条件,依次类推
      

  4.   

    这也太简单了吧
    var
      strCond : String;//查询条件////再次假定a、b、c、d都是字符型
      strCond :='';
      if edt1.Text <> '' then
        strCond := 'where a='''+edt1.text+''' '; 
      if edt2.Text <> '' then
      begin
        if strCond='' then
           strCond := 'where b='''+edt2.text+''' '
        else
           strCond:=strCond+' and b='''+edt2.text+''' '
      end;
      if edt3.Text <> '' then
      begin
        if strCond='' then
           strCond := 'where c='''+edt3.text+''' '
        else
           strCond:=strCond+' and c='''+edt3.text+''' '
      end;
      if edt4.Text <> '' then
      begin
        if strCond='' then
           strCond := 'where d='''+edt4.text+''' '
        else
           strCond:=strCond+' and d='''+edt4.text+''' '
      end;//应用查询条件
       sql.add('select ...... from  tableName '+strCond)
         
      

  5.   


    procedure TForm1.Button1Click(Sender: TObject);
    Const
       values:array[1..4] of string =(' AND 工号=',' AND 姓名=',' AND 部门=',' AND 职位=');
    var
       i:integer;
       str:string;
    begin   str:='select * from x_eng where 1=1';   for i := 1 to 4 do
         if trim(TEdit(FindComponent('Edit'+inttostr(i))).Text)<>'' then begin
           str:=str+values[i]+QuotedStr(TEdit(FindComponent('Edit'+inttostr(i))).Text);
         end;
        with Adoquery1 do begin
           close;
           sql.text:=str;
           open;
        end;
       
    end;