大约是这样:我有五个或更多的EDIT,如果五个都不为空的话就SELECT * FROM TABLE WHERE 条件1 LIKE EDIT1 AND 条件2 LIKE EDIT2......AND 条件5 LIKE EDIT5
如果只有EDIT2和EDIT3填了数据就SELECT * FROM TABLE WHERE 条件2 LIKE EDIT2 AND 条件3 LIKE EDIT3
意思是如果5个都不为空就有5个条件,只3个填EDIT的数据,就有3个条件,以此类推.
请问各位大侠,小弟应该如何判断和写代码呢?
小弟初学,请各位哥哥姐姐说详细点好吗?最好能有代码示范一下.谢谢了.

解决方案 »

  1.   

    就用if then判断是否有输入啊...到最后在sql.add中把那些值连接起来不就行了..
      

  2.   

    var
      a1,a2:string;
      sqlt,sqltemp:string;
    begin
      a1:=trim(edit1.text);
      a2:=trim(edit2.text);
      with adoquery1 do
        begin
          sqlt:='select * form 表名';
          if length(a1)>0 then
            sqltemp:='字段名 like ''%'+a1+'%''';
          if length(a2)>0 then
            sqltemp:=sqltemp+'字段名 like ''%'+a2+'%''';
          if length(sqltemp)>0 then
            begin
            if uppercase(copy(TrimLeft(sqltemp),1,2))<>'WHERE' then
               sqltemp:=' where '+sqltemp;
            if trim(uppercase(copy(TrimRight(sqltemp),length(TrimRight(sqltemp))-3,length(TrimRight(sqltemp)))))='AND' then
               sqltemp:=copy(TrimRight(sqltemp),1,length(TrimRight(sqltemp))-3);
          end;
          
          Close;
          SQL.Clear;
          SQL.Add(sqlt+sqltemp);
          Open;
        end;
    end;
      

  3.   

    上面少加了一个andsqltemp:='字段名 like ''%'+a1+'%'' and';
      

  4.   

    楼上的朋友,你上面只有二个EDIT啊...俺的是5个啊.算俺笨,看得不太懂.
      

  5.   

    好的,既然swcsoft(对不起,我踩到你尾巴了!) 和 myanwei(潜水员) 都说行,那我今晚就回去测试一下.行了再回来结贴.谢谢大家.如果还有朋友有更好的建议请贴出来,小弟感激不尽.
      

  6.   

    你用filter就好了.不管有多少个EDIT都可以将符合条件的记录查询出来  
    str := '';
      if combobox2.Text <> '' then str := 'yhbh=''' + combobox2.text + '''';
      if combobox3.Text <> '' then
        if str <> '' then
          str := str + ' and sbbh=''' + combobox3.text + ''''
        else
          str := str + ' sbbh=''' + combobox3.text + '''';
      if edit3.Text <> '' then
        if str <> '' then
          str := str + ' and nd=''' + edit3.text + ''''
        else
          str := str + ' nd=''' + edit3.Text + '''';
      if combobox1.Text <> '' then
        if str <> '' then
          str := str + ' and yf=''' + combobox1.Text + ''''
        else
          str := str + ' yf=''' + combobox1.Text + '''';
      if RadioButton1.Checked = true then
      begin
        if str <> '' then
          str := str + ' and  sfycb =1'
        else
          str := str + '  sfycb =1';
      end;
      if RadioButton2.Checked = true then
      begin
        if str <> '' then
          str := str + ' and  sfycb =0'
        else
          str := str + '  sfycb =0';
      end;
      adoquery2.Filter := str;
      adoquery2.Filtered := true;