现在有一张表 5 个字段 对应5个EDIT控件 然后通过这5个EDIT控件的内容进行查询现在的情况是 如果有两个EDIT有内容 剩下的几个为空该怎么办 偶想根据不同的条件(比如1个为空,4个非空.2个为空,3个非空等等)写IF语句判断 可是EDIT控件太多了 情况太复杂了写出来太乱了 我想这个是最笨的方法了偶很菜 刚刚学会点DELPHI想请教一下高手 有没有更好的办法啊

解决方案 »

  1.   

    SQL.Text := 'select * from TableName where ';
    if Trim(Edit1.Text)<>'' then SQL.Text := SQL.Text + 'a=''' + Trim(Edit1.Text) + '''';
    if Trim(Edit2.Text)<>'' then SQL.Text := SQL.Text + 'b=''' + Trim(Edit2.Text) + '''';
    .....
      

  2.   

    var SQL:string;
    SQL := 'select * from TableName where 1=1 ';
    if Trim(Edit1.Text)<>'' then SQL := SQL + ' and ...
    if Trim(Edit2.Text)<>'' then SQL := SQL + ' and ...
      

  3.   

    SQL.Text := 'select * from TableName where 1=1 ';
    if Trim(Edit1.Text)<>'' then SQL.Text := SQL.Text + ' AND a=''' + Trim(Edit1.Text) + '''';
    if Trim(Edit2.Text)<>'' then SQL.Text := SQL.Text + ' AND b=''' + Trim(Edit2.Text) + '''';
    .....
      

  4.   

    可以模糊查询的话就不用这么费事:
    sql.text := format('select * from tablename where f1 like %s% and f2 like %s%...' ,[edit1.text,edit2.text...]);
      

  5.   

    sorry应该这样
    sql.text :=format('select * from tablename where f1 like %s and f2 like %s...' ,[edit1.text+'%',edit2.text+'%'])