我有3个checkbox
分别是时间、井号、隶属于
表名为lyybb
请问我在选择不同的checkbox,怎么写这个sql语句呢

解决方案 »

  1.   

    最简单的:
    sql: string;
    sql:='select *from lyybb where 1=1'
    if checkbox1.checked then
    sql:=sql+'and shijian >date';
    if checkbox2.checked then
     sql:=sql+' and jinghao=hao';
    if checkbox3.checked then
     sql:=sql+'and  lsuy=shyu';
    adoquery1.close;
    adoquery1.clear;
    adoquery1.sql.text=sql;
    adoquery1.open;
      

  2.   

    var
      sSql, sWhr: string;
    begin
      sSql := 'select * from lyybb';
      sWhr := '';
      if CheckBox1.Checked then
        sWhr := ' where 时间=' + 时间;
      if CheckBox2.Checked then
        if sWhr = '' then
          sWhr := ' where 井号=' + 井号
        else
          sWhr := sWhr + ' and 井号=' + 井号;
      if CheckBox3.Checked then
        if sWhr = '' then
          sWhr := ' where 隶属于=' + 隶属于
        else
          sWhr := sWhr + ' and 隶属于=' + 隶属于;
      sSql := sSql + sWhr;
      ...
    end;
      

  3.   

    var
      sSql, sWhr: string;
    begin
      sSql := 'select * from lyybb where 1=1';
      sWhr := '';
      if CheckBox1.Checked then sWhr := ' and  时间=' + 时间;
      if CheckBox2.Checked then sWhr := sWhr + ' and 井号=' + 井号;
      if CheckBox3.Checked then sWhr := sWhr + ' and 隶属于=' + 隶属于;
       sSql := sSql + sWhr;
      ....
    end;
      

  4.   

    那我这么写有问题吗
    str:='select select jh,jd,cw,sgdw,tldc,tlsd,pjdym,zqsj,pjzqcyy,pjzqcys,pjrcyy,pjrcys,ylcyy,ylcys,nlcyy,nlcys,bz,id,yhjs from lyybb where'
       sqlwhere:='';
       for i:=0 to rzPanel2.ControlCount-1 do
           begin
              if TCheckBox(rzPanel2.Controls[z]).Checked=true
                 begin
                     fieldname:=TCheckBox(rzPanel2.Controls[z]).text;
                     sqlwhere:=sqlwhere+'fieldname='''+x1+'''';
                 end;
           end;
       sql:=str+sqlwhere;
      

  5.   

    肯定有问题啦
    rzPanel2.Controls[z]
    不能指定是哪一个啊
    还是老老实实的写吧
      

  6.   

    当你for i:=0 to rzPanel2.ControlCount-1 do
           begin
              if TCheckBox(rzPanel2.Controls[z]).Checked=true
                 begin
                     fieldname:=TCheckBox(rzPanel2.Controls[z]).text;
                     sqlwhere:=sqlwhere+'fieldname='''+x1+'''';
                 end;
           end;
       sql:=str+sqlwhere;
    一个都不符合时,语句多了‘where’,当符合条件>2时,where子句中少 and
      

  7.   

    试试下面:
    str:='select select jh,jd,cw,sgdw,tldc,tlsd,pjdym,zqsj,pjzqcyy,pjzqcys,pjrcyy,pjrcys,ylcyy,ylcys,nlcyy,nlcys,bz,id,yhjs from lyybb'
       sqlwhere:='';
       for i:=0 to rzPanel2.ControlCount-1 do
           begin
              if TCheckBox(rzPanel2.Controls[z]).Checked=true
                 begin
                     fieldname:=TCheckBox(rzPanel2.Controls[z]).text;
                     if sqlwhere='' then
                         sqlwhere:=sqlwhere+' where '+fieldname+'='+#39+x1+#39
                     else
                        sqlwhere:=sqlwhere+' and '+fieldname+'='+#39+x1+#39;             end;
           end;
       if sqlwhere <>'' then
           sql:=str+sqlwhere;