edit1,edit2,edit3,edit4使用如下语句行不通:
var str1,str2,str3:string; 
begin 
  if Edit1.Text='' then 
    str1:='' 
  else 
    str1:=' and FieldName='''+Edit1.Text+''''; 
  if Edit2.Text='' then 
    str2:='' 
  else 
    str2:=' and FieldName='''+Edit2.Text+''''; 
  if Edit3.Text='' then 
    str3:='' 
  else 
    str3:=' and FieldName='''+Edit3.Text+''''; 
ADOQuery.Sql.Text:='select * from TableName where 1=1'+str1+str2+str3;
使用如下语句也行不通:
  procedure   TForm5.Button1Click(Sender:   TObject);   
  Var   
  Tkind,Tcbs,Tname,Twriter,e1,e2,e3,e4:string;   
  i,j,k:integer;   
  temp:boolean;   
  begin   
  e1:=edit1.text;   
  e2:=edit2.text;   
  e3:=edit3.Text;   
  e4:=edit4.text;   
  temp:=false;   
  if(e1<>'')then   
                      begin   
                          if(temp=false)then   
                          Tkind:='   and   mainbook.kind="'+e1   +     '"'     ;   
                          temp:=true;   
                      end   
                      else   Tkind:='   ';   
  if(e2<>'')then   
                      begin   
                      if(temp=false)then   
                      Twriter:='   and   mainbook.ze="'+e2     +   '"'      
                      else   
                      Twriter:='   and   mainbook.ze="'+e2   +'"';   
                      temp:=true;   
                      end   
                      else   Twriter:='   ';   
  if(e3<>'')then   
                      begin   
                      if(temp=false)then   
                      Tname:='   and   mainbook.name="'+e3   +'"'
                      else   
                      Tname:='   and   mainbook.name="'+e3+'"';
                      temp:=true;   
                      end   
                      else   Tname:='   ';   
    
  if(e4<>'')then   
                      begin   
                      if(temp=false)then   
                      Tcbs:='     and   mainbook.cbs="'+e4+'"'
                      else   
                      Tcbs:='   and   mainbook.cbs="'+e4+'"'; 
                      temp:=true;   
                      end   
                      else   Tcbs:='   ';   
  Query1.close;   
  Query1.SQL.clear;   
  Query1.SQL.Add('Select   *   ');
  Query1.SQL.Add('From   mainbook.db   ');  
    
  Query1.SQL.Add('where   1=1')   ;
    
  Query1.SQL.Add(Tkind+twriter+Tcbs+Tname); 
    
      
  Query1.Open   ;  
要求:如果edit1为空,不查询此字段,依此类推;四个edit全为空,查询整张表,只要其中有一个为空就不查询此字段。

解决方案 »

  1.   

    把三个条件用一个字符串来组合,那样判断该字符串是否为空,如果为空,就去掉where
      

  2.   


    var str:string;
    begin
      str :='1=1';
      if Edit1.Text<>'' then
        str:=str+' and FieldName='''+Edit1.Text+'''';
      if Edit2.Text<>'' then
        str:=str+' and FieldName='''+Edit2.Text+'''';
      if Edit3.Text<>'' then
        str:=str+' and FieldName='''+Edit3.Text+'''';
    ADOQuery.Sql.Text:='select * from TableName where'+str;
      

  3.   

    var str:string;
    begin
      str := '';
      if edit1.text<>'' then str := ' fieldName='+#39+edit1.text+#39;
      if edit2.text<>'' then begin
        if str<>'' then str := str+' and ';
        str := str +' fieldName='+#39+edit2.text+#39;
      end;
      if edit3.text<>'' then begin
        if str<>'' then str := str+' and ';
        str := str +' fieldName='+#39+edit3.text+#39;
      end;
      if str<>'' then str := ' where '+str;
      ADOQuery.Sql.Text:='select * from TableName '+str;
    end;
      

  4.   

    begin
      if Edit1.Text<>'' then
        str:=str+' and FieldName='''+Edit1.Text+'''';
      if Edit2.Text<>'' then
        str:=str+' and FieldName='''+Edit2.Text+'''';
      if Edit3.Text<>'' then
        str:=str+' and FieldName='''+Edit3.Text+'''';
      ADOQuery.Sql.Text:='select * from TableName where 1=1 '+str;
    end;