strsql:='select * from table1 where ';
if (edit1.text<>''then strsql:=strsql+' d1='+edit1.text+'AND';
if (edit.text<>''then strsql:=strsql+'d1='+edit1.text+'AND d2='+edit2.text+'AND';
if <> then strsql:=strsql+'d1='+edit1.text+'AND';
...
strsql:=copy(strsql,1,length(strsql)-3);

解决方案 »

  1.   

    eulb:不好意思,‘and’是笔误。
      

  2.   

    这种算法肯定不行
    1,记录与查询条件一多,慢死了
    2,一旦出现所有的edit都为空,where就是多余的
    抱歉,好的我还没想到
      

  3.   

    我的一个例子,你要是嫌慢,那就没办法了 。procedure TForm1.queryBitBtnClick(Sender: TObject);
    var
      dateOk,keyOk,vipOk :Boolean ;//判断是否用日期、关键字进行过滤
    begin
      dateOk :=(dateGroupBox.font.Color =clGreen);
      keyOk :=(keyGroupBox.font.Color =clGreen);
      vipOk :=vipCheckBox.Checked ;  with diaryAdoQuery do begin
        close;
        sql.clear;
        sql.add('select * from ');
        sql.add(iTableName);
        sql.add(' where 名称=:名称');
        if dateOk then sql.add(' and 日期 between :startDate and :stopDate');
        if vipOk then sql.add(' and ok =:ok');
        if keyOk then sql.add(' and 内容 like ''%'+keyEdit.text+'%''');
        if dateOk then begin
          parameters.paramByName('startDate').value :=startDateTimePicker.DateTime ;
          parameters.paramByName('stopDate').value :=stopDateTimePicker.DateTime ;
        end;
        if vipOk then
          parameters.paramByName('ok').value :=true ;
        parameters.paramByName('名称').value :=iCaption;
        open;
        Last ;
      end;
      queryBitBtn.Enabled :=false;
      noQueryBitBtn.Enabled :=true;
    end;
      

  4.   

    这个方法如何?
    with Form1 do
      begin
       for i:=0 to ControlCount-1 do
        begin
         if (Controls[i] is TComboBox) then
         if (Controls[i] as TComboBox).Text<>'' then
         begin
          if get<>'' then
          get:=get+' and ';
          get:=get+(Controls[i] as TComboBox).Name+'='''+(Controls[i] as TComboBox).Text+'''';
         end;
         if (Controls[i] is TEdit) then
         if (Controls[i] as TEdit).Text<>'' then
         begin
          if get<>'' then get:=get+' and ';
          if isNumber((Controls[i] as TEdit).Text) then
          get:=get+(Controls[i] as TEdit).Name+'='+(Controls[i] as TEdit).Text
          else
          get:=get+(Controls[i] as TEdit).Name+'='''+(Controls[i] as TEdit).Text+'''';
         end;
        end;
      end;
     if get<>'' then
      strsql:='select * from table1 where '+get
     else 
      strsql:='select * from table1';
      

  5.   

    如果是edit用like子句
    如果是combobox(style=DropDownList),直接传递参数!
    试试吧