各位高手,怎样进行多个条件的查询呢?比如有四个条件,条件有可能全部为空,也可能全都有,怎么进行查询?给段例子

解决方案 »

  1.   

    用if来分别判断,最后将结果组成一个完整的SQL语句就行了!
      

  2.   

    SQL.Clear;
    SQL.add('... ');
    SQL.add('AND... ');
    SQL.add('AND...');
    ...
    ExecSQL;
      

  3.   

    也可以用if else if....的结构判断每条记录的字段值
      

  4.   

    IF EDIT1。TXT<>‘ ’THEN SQL.add('AND... ');
    IF EDIT2。TXT<>‘ ’THEN SQL.add('AND... ');
      

  5.   

    if 条件1 then
      sql:=sql+条件1;
    if sql<>'' then
      sql:=sql+'and'+条件2
      sql:=sql+条件2
      

  6.   

    只有四个条件还不简单?if……then……else
      

  7.   

    用if判断,除了第一个条件外都要加And,因此你的第一个条件有没有and要分析一下!
      

  8.   

    if... then... else
    if 条件1 then sql.add('and ...')
    if 条件2then sql.add('and ...')
    if 条件3then sql.add('and ...')
    if 条件41 then sql.add('and ...')
      

  9.   

    f:bool;
    f:=false;
    if 条件1 or 条件2 or 条件3 or 条件4 then
    sql:= sql+ 'where';
    else return sql;
    if 条件1 then
    begin
      sql:=sql+条件1;
      f:= true;
    end
    if 条件2 then
    begin
      if f then 
      sql:=sql+' and ' + 条件2;
      else
      sql:=sql+条件2;
      f:= true;
    endif 条件3 then
    begin
      if f then 
      sql:=sql+' and ' + 条件3;
      else
      sql:=sql+条件3;
      f:= true;
    end
    if 条件4 then
    begin
      if f then 
      sql:=sql+' and ' + 条件4;
      else
      sql:=sql+条件4;
      f:= true;
    end
    return sql
      

  10.   

    begin
                         adoquery1.close;
                         adoquery1.sql.clear;
                         adoquery1.sql.add('select * from 教师表');
                         if edit1.Text<>  then
                            adoquery1.SQL.Add('where 教师编号:='''+edit1.text'''');
                         if edit2.Text<>  then
                            adoquery1.SQL.Add('and 教师姓名:='''edit2.text'''');
                         if combobox1.Text<> then
                            adoquery1.SQL.Add('and 所属院系:='''combobox1.text');
                         if combobox2.Text<> then
                            adoquery1.SQL.Add('and 任教科目:='''combobox2.text'''');                     adoquery1.open;
                    end;
    这样写可以么?
      

  11.   

    用TCheckBox做选择,
    用if ... then ...做判断就可
      

  12.   

    wherestr:=' where 1 = 1';
    if 条件1 <>  '' then
    wherestr:= wherestr+' and .....';
    if 条件2 <>  '' then
    wherestr:= wherestr+' and .....';
    .......
      

  13.   

    //如果选择日期,加入日期条件
       if cbbdate.Checked =true then
          StrSql:=StrSql+'and sddate between '+'#'+datetostr(dtpsfdate.Date)+
                  '#'+' and '+'#'+datetostr(dtpsfend.Date)+'#';
       //如果选择客户
       if cbkh1.Checked=true then
        begin
          adofind.SQL.Clear;
          adofind.SQL.Add('select * from khxx where khmc='''+cbkh.Text+'''');
          adofind.Open;
          StrSql:=StrSql+'and khdm='''+adofind.fieldbyname('khdm').AsString+'''';
        end;
        //如果选择表名
        if cbbm1.Checked=true then
        begin
          adofind.SQL.Clear;
          adofind.SQL.Add('select * from khbh where khbm='''+cbbm.Text+'''');
          adofind.Open;
          StrSql:=StrSql+'and khbh='''+adofind.fieldbyname('khbh').AsString+'''';
        end;
        //如果选择收费员
        if cbsfy1.Checked=true then
        begin
          adofind.SQL.Clear;
          adofind.SQL.Add('select * from czyb where czyxm='''+cbsfy.Text+'''');
          adofind.Open;
          StrSql:=StrSql+'and ysf='''+adofind.fieldbyname('czybm').AsString+'''';
        end;
        //如果选择开票员
        if cbkpy1.Checked=true then
        begin
          adofind.SQL.Clear;
          adofind.SQL.Add('select * from czyb where czyxm='''+cbkpy.Text+'''');
          adofind.Open;
          StrSql:=StrSql+'and ykp='''+adofind.fieldbyname('czybm').AsString+'''';
        end;
        //如果是选择水费或者电费
        if rbsf.Checked=true then
           begin
             StrSql:=StrSql+' and khbh like '''+'w%'+'''';
           end
        else
           begin
             StrSql:=StrSql+' and khbh like '''+'e%'+'''';
           end;
        //开始查找数据
        StrSql:=copy(StrSql,5,255);
        if StrSql='' then
          begin
             showmessage('请选择一个查询条件,在其前面打钩即可');
             exit;
          end;
        with dmmain.ADOQKhsf do
           begin
             sql.Clear;
             sql.Add('select * from khsf where');
             sql.Add(StrSql);
             open;
           end;