Delphi多条件查询格式

定义sql

var

    sql:string;


查询语句

 DataModule1.worksdata.CommandText:='select * from 送气工 where '+sql+'';


查询多条件判断

if(flag = false)   then
begin
    if(ComboBox2.Text = '等于') then
    begin
      sql:=''+trim(ComboBox1.Text)+' = '''+trim(Edit1.Text)+'''';
    end
    else if(ComboBox2.Text = '包含') then
    begin
      sql:=''+trim(ComboBox1.Text)+'  like '''+'%'+trim(Edit1.Text)+'%'+'''';

    end;

end

else if(flag =true) then
begin
    if(ComboBox2.Text = '等于') then
    begin
      if(ComboBox3.Text = '并且') then
      begin
        sql:=sql+ 'and' +' ' + ''+trim(ComboBox1.Text)+'  = '''+'%'+trim(Edit1.Text)+'%'+'''';
      end
      else if(ComboBox3.Text = '或者') then
      begin
        sql:=sql+ 'or' +' ' + ''+trim(ComboBox1.Text)+'  = '''+'%'+trim(Edit1.Text)+'%'+'''';
      end
      else if(ComboBox3.Text = '排除') then
      begin
        sql:=sql+ 'and' +' ' +''+trim(ComboBox1.Text)+'  <> '''+'%'+trim(Edit1.Text)+'%'+'''';
      end;
    end
    else if(ComboBox2.Text = '包含') then
    begin
      if(ComboBox3.Text = '并且') then
      begin
        sql:=sql+ 'and' +' ' + ''+trim(ComboBox1.Text)+'  like '''+'%'+trim(Edit1.Text)+'%'+'''';
      end
      else if(ComboBox3.Text = '或者') then
      begin
        sql:=sql+ 'or' +' ' + ''+trim(ComboBox1.Text)+'  like '''+'%'+trim(Edit1.Text)+'%'+'''';
      end
      else if(ComboBox3.Text = '排除') then
      begin
        sql:=sql +'and'+' '+''+trim(ComboBox1.Text)+' not like '''+'%'+trim(Edit1.Text)+'%'+'''';
      end;
    end;
 end;


你可能感兴趣的:(Delphi多条件查询格式)