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) + ''''; .....
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 ...
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) + ''''; .....
可以模糊查询的话就不用这么费事: sql.text := format('select * from tablename where f1 like %s% and f2 like %s%...' ,[edit1.text,edit2.text...]);
sorry应该这样 sql.text :=format('select * from tablename where f1 like %s and f2 like %s...' ,[edit1.text+'%',edit2.text+'%'])
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) + '''';
.....
SQL := 'select * from TableName where 1=1 ';
if Trim(Edit1.Text)<>'' then SQL := SQL + ' and ...
if Trim(Edit2.Text)<>'' then SQL := SQL + ' and ...
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) + '''';
.....
sql.text := format('select * from tablename where f1 like %s% and f2 like %s%...' ,[edit1.text,edit2.text...]);
sql.text :=format('select * from tablename where f1 like %s and f2 like %s...' ,[edit1.text+'%',edit2.text+'%'])