procedure TForm1.Button1Click(Sender: TObject);
var
sqlstr:String;
s:String; //条件字符串
begin
sqlstr:='select * from tablename';
s:='';
if Edit1.Text<>'' then s:=s+' ID='''+Edit1.Text+''' and';
if Edit2.Text<>'' then s:=s+' NAME='''+Edit2.Text+''' and';
if Edit3.Text<>'' then s:=s+' ADDRESS='''+Edit3.Text+''' and';
{... 其他4个条件类似}
if s<>'' then
begin
s:=copy(s,1,length(s)-4); //去掉最后一个and
sqlstr:=sqlstr+' where'+s;
end;
query1.Close;
query1.SQL.Clear;
query1.SQL.Text:=sqlstr;
query1.Open;
end;
with query1 do begin
sql.add('select * from xxx where 1=1');
if edit1.text<>'' then
sql.add('and id='''+edit1.text+'''');
if edit2.text<>'' then
sql.add('and name='''+edit2.text+'''');
//...以此类推
close;
open;
end;